//d.file是上传的原始文件数据
let quality=1
if(d.file.size<1000*1000){ //小于1M
quality=0.8
}else if(d.file.size<5000*1000){ //小于5M
quality=0.5
}else if(d.file.size<10000*1000){ //小于10M
quality=0.3
}else{ //大于10M
quality=0.1
}
new Compressor(d.file, {
quality: quality, //压缩比例0-1
success:(result) => {
// console.log(result);
//blob转file,因为大文件会被转成blob类型
result = new File([result], d.file.name, {type: d.file.type, lastModified: Date.now()});//blob转file
var params = new FormData();
params.append('file', result);
this.$http
.post("/appFile/fileUpload",params)
.then((res) => {
console.log(res);
})
.catch((err) => {
console.error(err);
});
},
error(err) {
console.log(err.message);
},
});
参考compressorjs
|