我编写了一段可以运行的代码,但它不是杰作。它存在巨大的性能问题。我该如何处理呢?
问题
我的组件的性能存在问题:
发送多张图片很慢,当我发送5张图片时,大约需要1-2分钟。
我如何确保照片仅在提交期间上传到 Cloudinary?因为,例如,当用户选择照片 A,然后环顾四周并选择照片 B 时,照片 A 和 B 都会上传到云端。
问题
在将文件传输到 Cloudinary 时禁用添加相册的按钮是个好主意吗?
请帮助我解决这两个问题,我想确保应用程序顺利运行。太感谢了。
const AddAlbum = () => {
const [fileUrl, setFileUrl] = useState();
// MultipleFiles
const [multipleFileUrl, setMultipleFileUrl] = useState([]);
// cloudnary upload api endpoint
const api_cloudinary =
"https://api.cloudinary.com/v1_1/cloudinary_name/image/upload";
// file upload
const handleSingleFileUpload = async (files) => {
const formData = new FormData();
formData.append("file", files[0]);
formData.append("upload_preset", "preset_number");
// send cloudinary image and presets info
const res = await fetch(api_cloudinary, {
method: "POST",
body: formData,
});
const file = await res.json();
console.log(file);
const fileUrl = await file.eager[0].url;
setFileUrl(fileUrl);
};
console.log(fileUrl);
// upload many files cloudnary
// For now max amount of files is 20
const handleMultipleFileUpload = async (files, amount) => {
const formData = new FormData();
for (let i = 0; i <= files.length; i++) {
let file = files[i];
formData.append("file", file);
formData.append("upload_preset", ""preset_number"");
const res = await fetch(api_cloudinary, {
method: "POST",
body: formData,
});
const cloudinaryFiles = await res.json();
console.log(cloudinaryFiles);
setMultipleFileUrl(cloudinaryFiles);
}
};
const handleSubmit = async (e) => {
e.preventDefault();
console.log(
`
Album name:${albumName}
color:${color}
Background Image: ${fileUrl}
files:${multipleFileUrl}
`
);
};
慕尼黑的夜晚无繁华
呼啦一阵风
相关分类