uview u-upload 添加微信文件上传方式

u-upload.vue
34行改为

<view style="display: inline-block;" @tap="showSelectFile" v-if="maxCount > lists.length">

添加popup组件

		<u-popup v-model="showPopup" mode="bottom" ref="filePopUp">
			<u-grid :col="2">
				<u-grid-item @tap="selectFile">
					<u-icon name="file-text" :size="46"></u-icon>
					<view class="grid-text">从文件选择</view>
				</u-grid-item>
				<u-grid-item @tap="selectMessageFile">
					<u-icon name="chat" :size="46"></u-icon>
					<view class="grid-text">从聊天文件选择</view>
				</u-grid-item>
			</u-grid>
		</u-popup>

props 添加

//文件类型 all-所有文件,video-视频文件,image-图片文件,file-其他文件
		fileType: {
			type: String,
			default() {
				return 'image'
			}
		},

data 添加

showPopup: false,//弹窗

methods 添加

//选择上传的文件
		showSelectFile(){
			this.showPopup = true
		},
		//选择聊天记录文件
		selectMessageFile(){
			this.showPopup = false
			if (this.disabled) return;
			const { name = '', maxCount, multiple, maxSize, sizeType, lists, camera, compressed, maxDuration, sourceType, fileType } = this;
			let chooseFile = null;
			const newMaxCount = maxCount - lists.length;
			// 设置为只选择图片的时候使用 chooseImage 来实现
			chooseFile = new Promise((resolve, reject) => {
				//选择聊天文件
				wx.chooseMessageFile({
				  count: multiple ? (newMaxCount > 9 ? 9 : newMaxCount) : 1,
				  type: fileType,
				  success: resolve,
				  fail: reject
				})
			});
			chooseFile
				.then(res => {
					let file = null;
					let listOldLength = this.lists.length;
					res.tempFiles.map((val, index) => {
						// 检查文件后缀是否允许,如果不在this.limitType内,就会返回false
						if(!this.checkFileExt(val)) return ;
						
						// 如果是非多选,index大于等于1或者超出最大限制数量时,不处理
						if (!multiple && index >= 1) return;
						if (val.size > maxSize) {
							this.$emit('on-oversize', val, this.lists, this.index);
							this.showToast('超出允许的文件大小');
						} else {
							if (maxCount <= lists.length) {
								this.$emit('on-exceed', val, this.lists, this.index);
								this.showToast('超出最大允许的文件个数');
								return;
							}
							lists.push({
								url: val.path,
								progress: 0,
								error: false,
								file: val
							});
						}
					});
					// 每次图片选择完,抛出一个事件,并将当前内部选择的图片数组抛出去
					this.$emit('on-choose-complete', this.lists, this.index);
					if (this.autoUpload) this.uploadFile(listOldLength);
				})
				.catch(error => {
					this.$emit('on-choose-fail', error);
				});
		},

selectFile()首行添加

this.showPopup = false

在这里插入图片描述

小功告成

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值