onChange = (files, type, index) => {
const newItem = _.cloneDeep(this.state.imgData);
if (type === "add") {
const file = [...files].pop().file;
const newitem = [...files].pop();
const handle = this.handleUPdata;
if (file.size > 10 * 1024 * 1024) {
Toast.fail("请上传10M以下的图片!");
return;
}
this.setState({
files,
loading: true,
});
if (file.size > 1 * 1024 * 1024) {
let rate = 0.2;
let reader = new FileReader();
reader.readAsDataURL(file);
let img = new Image();
reader.onload = function (e) {
img.src = e.target.result;
};
img.onload = function () {
let canvas = document.createElement("canvas");
let context = canvas.getContext("2d");
const fileSizeKB = Math.floor(file.size / 1024);
if (fileSizeKB * rate > 3027) {
rate = Math.floor((3027 / fileSizeKB) * 10) / 30;
}
let targetW = (canvas.width = this.width * rate);
let targetH = (canvas.height = this.height * rate);
context.clearRect(0, 0, targetW, targetH);
context.drawImage(img, 0, 0, targetW, targetH);
canvas.toBlob((blob) => {
const newImage = new File([blob], file.name, {
type: file.type,
});
handle([{ file: newImage }]);
});
};
} else {
handle(files);
}
} else {
}
};