先引入 npm i image-conversion
然后在页面中引用 import * as imageConvsersion from "image-conversion";
在代码中使用
new Promise((resolve, reject) => {
console.log("压缩前", res.raw); // 压缩到300KB,大于300KB的图片都会进行压缩,小于则不会此值可自行定义
imageConvsersion.compressAccurately(res.raw, 300).then((res1) => {
res1 = new File([res1], res.name, {
type: res1.type,
lastModified: Date.now(),
});
console.log("压缩后", res1);
// resolve(res);
this.img = res1;
});
});
注意压缩的文件是二进制格式 得到的也是二进制格式