uView上传组件upload上传auto-upload模式图片压缩

诸腾
2023-12-01

参照手动上传的代码改的
https://blog.csdn.net/weixin_43841757/article/details/111040124

上传前的钩子before-upload
html

<u-upload :auto-upload="true"
:before-upload="onBeforeUpload" ref="uUpload1" @on-choose-complete="onChooseComplete(1)"
....略其它
</u-upload>

js

onBeforeUpload(index1, list){
				this.isuploading = "正在上传中....";
				let self = this
				console.log("onBeforeUpload before==========>>>",list[index1].file.size/1024/1024)
				// #ifdef H5
				return this.$compressImg(list[index1].file )//调用compress
				.then(res =>{
					let lists =null
					if(self.act == 1){
						lists = self.$refs.uUpload1.lists
					}
					if(self.act == 2){
						lists = self.$refs.uUpload2.lists
					}
					if( lists ){
						lists.forEach((item, index) => {
							if (item.file.name == res.name) {
								console.log("compress upload==========>>>",res,res.size/1024/1024)
								lists[index].file = res;
								lists[index].url = res.path;
							}
						})
					}
				})
				// #endif
			}
 类似资料: