我正在从浏览器直接向 Cloudinary 进行未签名的上传,并试图显示大型上传的进度。
我正在使用 cloudinary node library .
有没有什么方法可以在浏览器中使用 cloudinary 库获取上传进度,或者有其他方法可以直接上传到 cloudinary 给我进度吗?
最佳答案
我最终通过 XHR 请求上传解决了这个问题,而不是依赖于 cloudinary 的库:
const formData = new FormData();
formData.append('upload_preset', CLOUDINARY_MEDIA_PRESET);
formData.append("file", file);
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(e) {
if(xhr.readyState !== 4) {
return;
}
if(xhr.status !== 200) {
// Handle request errors
return;
}
let res = JSON.parse(xhr.responseText);
if(!res || !res.public_id) {
// Handle cloudinary errors
return;
}
// Handle success
};
xhr.upload.addEventListener("progress", e => {
// Handle progress with Math.round((e.loaded * 100.0) / e.total)
});
xhr.open("post", `https://api.cloudinary.com/v1_1/${CLOUDINARY_CLOUD}/auto/upload`);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.send(formData);
关于cloudinary - 获取 Cloudinary Unsigned Upload 的上传进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51881345/