图片上传最大张数限制
是的,既然图片是多张上传,肯定需要数量限制嘛,不然整个几百张那也得炸… 因此可以用到max-count这个方法限制,具体方法如下:
<van-uploader v-model="fileList" :multiple="multipleFalse" max-count="20" preview-size="9.33vw" :after-read="handlerAfterRead" upload-icon="https://kcallme-mall.oss-cn-beijing.aliyuncs.com/images/8508f79b2da948c6a383c5a8bac0faf1..png">
<div class="pic_panel append_wall_btn">
<img class="append_wall_btn_2" src="https://kcallme-mall.oss-cn-beijing.aliyuncs.com/images/8508f79b2da948c6a383c5a8bac0faf1..png">
</div>
</van-uploader>
上传方法
官方给出的上传回调有很多种,before-read after-read before-delete 结合需求以及代码优化 我们这里只用到了 after-read 亲测好用
:after-read="handlerAfterRead"
async handlerAfterRead(file,detail){
if(Array.isArray(file)){
//这次真是要吐槽一下vant组件 真心没有element好用
//element 上传地址可以直接写到组件里 vant还要在方法里写上传的接口地址
file.forEach((item,index)=>{
let data = new FormData();
data.append('file', item.file, item.file.name);
const that = this;
const token = localStorage.getItem("token");
$.ajax({
url: process.env.baseUrl + '/file/upload/imgs',
type: 'POST',
data: data,
cache: false,
headers:{
Authorization: "" + token
},
processData: false,
contentType: false,
success:function(d){
console.log("d=>",d.data);
that.groupForm.fileList.push(d.data);
if(that.groupForm.fileList.length >=4){
that.hasFourImg = 'position:absolute;right:0'
}
console.log('====',that.groupForm.fileList)
}
});
return true;
})
}else{
var data = new FormData();
data.append('file', file.file);
const that = this;
const token = localStorage.getItem("token");
$.ajax({
url: process.env.baseUrl + '/file/upload/img',
type: 'POST',
data: data,
cache: false,
headers:{
Authorization: "" + token
},
processData: false,
contentType: false,
success:function(d){
console.log("d=>",d.data);
that.groupForm.fileList.push(d.data);
if(that.groupForm.fileList.length >=4){
that.hasFourImg = 'position:absolute;right:0'
}
}
});
}
},