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
小功告成