Javascript 将Dropzone组件与图像压缩相结合
我正在尝试使用React-dropzone组件将dropzone集成到React中。我现在面临的问题是,我需要在上传之前压缩图像。所以,我在网上搜索,找到了一个解决方案,我必须用自己的方法重写基本的图片上传。这是:Javascript 将Dropzone组件与图像压缩相结合,javascript,reactjs,dropzone.js,react-dropzone,Javascript,Reactjs,Dropzone.js,React Dropzone,我正在尝试使用React-dropzone组件将dropzone集成到React中。我现在面临的问题是,我需要在上传之前压缩图像。所以,我在网上搜索,找到了一个解决方案,我必须用自己的方法重写基本的图片上传。这是: const eventHandlers = { error: () => (setSubmit(false), setSubmitText(t('common.interupted'))), canceled: () => (setSubmit(false), setSu
const eventHandlers = {
error: () => (setSubmit(false), setSubmitText(t('common.interupted'))),
canceled: () => (setSubmit(false), setSubmitText(t('common.interupted'))),
complete: (resp, file) => {
setPictures({ ids: [...pictures.ids, resp.data.id] });
const progressElement = file.previewElement.querySelector('.dz-progress');
progressElement.style.display = 'none';
const successMark = file.previewElement.querySelector('.dz-success-mark');
successMark.style.display = 'visible';
},
queuecomplete: () => (setSubmit(false), setSubmitText(t('common.submit'))),
processing: async (file) => {
const newfile = await resizeImageFn(file);
file = {
...file,
blob: newfile,
};
setSubmit(true);
setSubmitText(t('common.processing'));
await eventHandlers.sending(file);
},
sending: async (file) => {
console.log(file);
console.log('Upload 1 file start');
const apiUrl = wordPressSiteUrl + 'wp-json/wp/v2/media/';
const formData = new FormData();
const fileType = 'jpeg';
const randNumber1 = Math.floor(Math.random() * 1000);
const randNumber2 = Math.floor(Math.random() * 1000);
formData.append(
'file',
file.blob,
`WM-${randNumber1}${randNumber2}.${fileType}`
);
try {
const resp = await axios.post(apiUrl, formData, {
headers: {
Authorization: `Bearer ${authToken}`,
'content-type': 'multipart/form-data',
},
});
if (resp.status !== 200 && resp.status !== 201) {
cookies.remove('access', { path: '/' });
window.location.href = '/';
eventHandlers.error();
} else {
eventHandlers.complete(resp, file);
}
} catch (e) {
eventHandlers.error();
}
},
addedfile: async (file) => {
await eventHandlers.processing(file);
},
thumbnail: function (file) {
const progressElement = file.previewElement.querySelector(
'[data-dz-uploadprogress]'
);
progressElement.style.width = '100%';
},
})
问题是,它一次上传所有的图像,有时服务器会停止响应。所以,我的问题是:我可以集成图像压缩来响应dropzone组件而不改变它上载文件的方式吗?它2个2个上传,这对我来说真的是最好的解决方案。或者你可以建议我,我怎样才能一张一张地上传图片。我知道如何依次进行,但不知道如何逐个上传;)
先谢谢你