beforeAvatarUpload() {//上传前的钩子
return new Promise((resolve) => {
let that = this;
async function dbFuc() {
for (let i = 0; i < that.File_List.length; i++) {
let isLt2M = that.File_List[i].size / 1024 / 1024 < 1;
if (!isLt2M) {
// 压缩到0.9m,这里的900就是要压缩的大小,可自定义
let File_l = that.File_List[i];
await imageConversion
.compressAccurately(File_l.raw, 900)
.then((res) => {
res = new File([res], File_l.name, {
type: res.type,
lastModified: Date.now(),
});
that.File_List[i].raw = res;
that.File_List[i].size = res.size;
});
}
}
resolve();
}
dbFuc();
});
},
submitUpload() {
//手动上传
this.beforeAvatarUpload().then(() => {
const formList = new FormData();
formList.append("token", succ.result);
this.File_List.forEach((file) => {
console.log(file);
// 因为要上传多个文件,所以需要遍历
formList.append("file", file.raw);
});
console.log(formList.getAll("file"));
return;
this.axios
.post("/api/v1.0/files/upload/up", formList)
.then((ress) => {});
});
}
vue循环压缩图片后多张一起上传的异步解决
最新推荐文章于 2024-01-25 16:05:24 发布