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

uni-file-picker文件预览

一、介绍

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

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

二、代码

一般小程序引入的组件都存放在uni_modules目录下(需要修改uni-file-picker.vueupload-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'],
相关推荐
m0_4628038810 小时前
从信息管理视角看「按桌顺序分桌」:云分组优先分组的实现逻辑
微信小程序
爱折腾的编程老炮15 小时前
潮玩品牌自建抽盒机小程序——商城 + 抽盒 + 一番赏 + 会员 + 社区,一套代码怎么搭
spring boot·小程序·vue·mybatis·uniapp
新知图书15 小时前
第13章 从一句话需求到上线“续费管家”小程序
人工智能·小程序·智能体
数商云企1 天前
2026年陕西软件开发首选数商云企AI微入口小程序定制方案
人工智能·小程序
bug总结1 天前
uniapp 微信小程序分包规范
微信小程序·小程序·uni-app
Quor1 天前
小程序工作室:AI 生成即运行的可视化工作台
人工智能·小程序
盟道科技2 天前
小程序电商订单超时自动取消的三种实现方案:定时扫描、Redis 过期监听、延迟消息对比与生产落地
数据库·redis·小程序
lhldsg2 天前
智慧场馆解决方案小程序系统:从架构设计到落地实践
java·小程序·需求分析
凉红茶2 天前
用Cursor开发微信小程序的第一天
微信小程序·小程序·ai编程
CRMEB系统商城2 天前
汽车养护连锁的数字化底座CRMEB 多门店系统实战方案
java·开发语言·小程序·开源·汽车