【问题标题】:How to download BLOB after compression压缩后如何下载BLOB
【发布时间】:2022-01-04 14:00:14
【问题描述】:

我使用 dropzone.js 和图像压缩库。但我很难下载 blob。任何建议我该怎么做。下面是我的代码

// "myDropzone" is the camelized version of the HTML element's ID
Dropzone.options.myDropzone = {
 url:" ",
  transformFile: function(file, done) {
     const imageCompressor = new ImageCompressor();

     imageCompressor.compress(file, {
     // I assume the output image won't have the meta data anymore
     checkOrientation: true,
     // Limit output image width & height
     // For controllable file size & avoid blank output image
     // https://github.com/xkeshi/image-compressor#maxwidth
     maxWidth: 8192,
     maxHeight: 8192,
     // 0.8 is the default and already good enough
     quality: 0.6,
     // Convert ALL PNG images to JPEG
     convertSize: 0,
     })
     .then((result) => {
       // Handle the compressed image file.
       done(result)
console.log(result);

     })
     .catch((err) => {
       // Handle the error
       throw err
     })
  }
};

我尝试创建一个下载链接并下载“结果”,但我得到了损坏的文件

【问题讨论】:

    标签: javascript jquery dropzone.js


    【解决方案1】:

    我自己找到了解决方案。我创建了 blob 的 ObjectURL,然后我创建了一个“a”元素

        const url = window.URL.createObjectURL(result);
        const a = document.createElement('a');
        a.style.display = 'none';
        a.href = url;
        // the filename you want
        a.download = result.name;
        document.body.appendChild(a);
        a.click();
       window.URL.revokeObjectURL(url);
    

    【讨论】:

      猜你喜欢
      • 2014-08-08
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多