微信小程序【uni-file-picker文件预览】

uni-file-picker文件预览

一、介绍

这个组件上传文件回显,点击预览都正常,然后换成上传文件就凉凉,只能上传不能回显,完全不晓得自己上传的对不对

使用组件uni-file-picker文章:https://blog.csdn.net/weixin_45853881/article/details/156019921

二、代码

一般小程序引入的组件都存放在uni_modules目录下(需要修改uni-file-picker.vue和upload-file.vue文件)

找到当前项目的uni_modules\uni-file-picker\components\uni-file-picker目录下

1、修改uni-file-picker.vue

在文件19行左右加入@preview="preview"

bash 复制代码
<upload-file v-if="fileMediatype !== 'image' || showType !== 'grid'" :readonly="readonly"
	:list-styles="listStyles" :files-list="filesList" :showType="showType" :delIcon="delIcon"
	@uploadFiles="uploadFiles" @choose="choose" @delFile="delFile" @preview="preview">
	<slot><button type="primary" size="mini">选择文件</button></slot>
</upload-file>

在文件methods中加入方法preview,我实在delFile方法之后写的,也就是535行

bash 复制代码
/**
 * 文件预览(支持pdf/doc/docx)
 * @param {Object} file 文件对象 {name,url}
 */
preview(file) {
	if (!file || !file.url) {
		uni.showToast({
			title: "文件地址不存在",
			icon: "none"
		});
		return;
	}
	uni.downloadFile({
		url: file.url,
		success: (res) => {
			if (res.statusCode === 200) {
				uni.openDocument({
					filePath: res.tempFilePath,
					success: () => {},
					fail: () => {
						uni.showToast({
							title: "文件打开失败",
							icon: "none"
						});
					}
				})
			}
		},
		fail: () => {
			uni.showToast({
				title: "文件下载失败,无法预览",
				icon: "none"
			});
		}
	})
},

2、修改upload-file.vue

看表格看代码块都可以,写代码块是为了方便复制,反正我懒,现在写了以后省事

源代码 修改后 行数 截图
<view class="files__name">{``{item.name}}</view> <view class="files__name" @click="$emit('preview', item)">{``{item.name}}</view> 18行左右
emits:['uploadFiles','choose','delFile'], emits:'uploadFiles','choose','delFile','preview', 41行左右

2.1、增加点击事件

bash 复制代码
<view class="files__name">{{item.name}}</view>

修改为(增加点击事件)

bash 复制代码
<view class="files__name" @click="$emit('preview', item)">{{item.name}}</view>

2.2、增加抛出事件

bash 复制代码
emits:['uploadFiles','choose','delFile'],

修改为(增加preview)

bash 复制代码
emits:['uploadFiles','choose','delFile','preview'],
相关推荐
用户788477316346 小时前
一个门店预约小程序,真正难的不是页面,是这 4 个后端接口
微信小程序
老马识码6 小时前
一个围棋小程序里的七个 Agent:场景、提示词和结构化输出的门道
人工智能·微信小程序
拖孩6 小时前
一个人 + AI 做的小程序,一个半月把服务器钱赚回来一半了
前端·后端·微信小程序
前端小万6 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
MoonMoonLee6 天前
小程序配置
小程序
拖孩6 天前
代码我能全交给 AI,流量主这 500 个访客它一个都替不了我
前端·后端·微信小程序
2501_916007476 天前
苹果商店iOS应用上架费用全面解析:开发者计划与审核费用计算
android·ios·小程序·https·uni-app·iphone·webview
JQweryuiop6 天前
中小型游戏陪练工作室数字化管理实践:基于七彩屋电竞护航小程序订单、履约与绩效系统的落地复盘
大数据·游戏·小程序·架构
Ai-_Man6 天前
您您这可以把Mistral AI的多个会话比如说。左侧的多个会话一次性导出吗?不是单条会话里面的多次会对话。
人工智能·ai·小程序·电脑
2501_916008896 天前
如何实现iOS App代码混淆:SwiftShield使用指南
android·ios·小程序·https·uni-app·iphone·webview