cloudinary - 获取 Cloudinary Unsigned Upload 的上传进度

标签 cloudinary

我正在从浏览器直接向 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/

相关文章:

使用变量时,Angular Cloudinary 宽度不起作用

node.js - NodeJS 中的 cloudinary 文件上传遇到问题

javascript - 如何使用node.js从cloudinary下载图像

cloudinary - 始终收到错误 : wrong uploadSignature on Signed uploads Cloudinary Widget

android - 如何在 Android 中将文件上传到 Cloudinary 时显示进度?

javascript - 在脚本文本内容中 react setState

javascript - 将图像上传到 Cloudinary Express.js React Axios post 请求

javascript - public_id未定义 Meteor+Cloudinary

javascript - 云错误 : must supply api-key -/node_modules/cloudinary/lib/utils. js:982