我正在使用react-dropzone上传文件。
<Dropzone
accept="image/jpeg"
multiple
onDrop={(acceptedFiles, rejectedFiles) => this.onDrop(acceptedFiles, rejectedFiles)}
/>
在 onDrop 函数中,我缩放使用画布获得的每个图像。
onDrop(acceptedFiles: Array<Object>, rejectedFiles: Array<Object>) {
if (acceptedFiles.length) {
const requestParams = {
parameters: {
generateContourImages: false,
},
images: [],
};
acceptedFiles.forEach(async (acceptedFile) => {
const { width, height } = await browserImageSize(acceptedFile);
const imagedata = await this.scaleImage(acceptedFile, width, height);
requestParams.images.push({
imagedata,
methods: methods,
imageID: uuidv4(),
});
});
} else {
this.setState({ error: true });
}
}
实际缩放发生在下面的函数中
async scaleImage(fileImage: Object, originalWidth: number, originalHeight: number) {
return new Promise((resolve) => {
const imageURL = URL.createObjectURL(fileImage);
const image = new Image();
image.onload = () => {
const scaleFactor = Math.max(originalWidth, originalHeight) / 1000;
const canvas = document.createElement('canvas');
canvas.width = originalWidth;
canvas.height = originalHeight;
const scaledWidth = Math.floor(originalWidth / scaleFactor);
const scaledHeight = Math.floor(originalHeight / scaleFactor);
const ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0, scaledWidth, scaledHeight);
const base64 = canvas.toDataURL('image/jpeg');
resolve(base64.substr(23));
}
image.src = imageURL;
});
}
然后我构造的requestParams
对象将被发送到服务器。我的问题是我希望我的客户端代码等到它遍历所有图像并构造最终requestParams
对象。现在情况并非如此。如何让上面的代码等待?