微信小程序【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'],
相关推荐
源码手记1 小时前
基于微信小程序的旅游攻略系统-毕设源码
微信小程序·课程设计·旅游
源码手记6 小时前
基于微信小程序的美甲美睫预约系统-毕设源码
微信小程序·小程序·课程设计
微订校园外卖系统10 小时前
自营小程序低成本落地:交付链路建模与首单联调清单
小程序·外卖系统·本地生活·微订
微订外卖系统1 天前
门店小程序从零上线:单店实体建模与订单状态机梳理
小程序·外卖系统·本地生活·微订
西泽XIZ92 天前
微信小程序网络与数据库交互实践:从后端环境搭建到数据渲染
网络·数据库·微信小程序·小程序
知了学历孙老师2 天前
考研报名档案所在地填错,调档和录取会出什么岔子
考研·小程序
夜-未央2 天前
微信小程序多端插件获取OAID实践
微信小程序·小程序
AI源码2 天前
体育场馆预约小程序
小程序
CSharp精选营3 天前
17 天,改名 3 次、停服 7 天:个人主体做小程序,红线都在这
微信小程序·个人开发者·小程序合规·ugc红线·备案踩坑
橙序员小站3 天前
没有AI,我根本不知道同事有多苦
前端·微信小程序