【问题标题】:React-tsx-Download Multiple image file in Zip formatReact-tsx-以Zip格式下载多个图像文件
【发布时间】:2020-01-28 07:17:50
【问题描述】:

我想下载包含多个图像的 zip,我使用 js-zip 但它不允许我使用 jszip-util 它的显示找不到名称 JSZipUtils 因为我认为我的 TSX 文件。这是我尝试过的一些代码..

    import * as React from 'react';
    import  JSZip from 'jszip';
    import FileSaver from 'file-saver';

    class ZipDemo extends React.Component{
        download =()=>{
        let links:string[]=[
            "https://sequenceimagestaging.blob.core.windows.net/retouch/3xl/100/1610004/one.jpg?v=1569588865599",
             "https://sequenceimagestaging.blob.core.windows.net/retouch/3xl/900/2411015/two.jpg?v=1569588865599",
             "https://sequenceimagestaging.blob.core.windows.net/retouch/l/200/3310029/three.jpg?v=1569588865599",
             "https://sequenceimagestaging.blob.core.windows.net/retouch/m/300/3237036/four.jpg?v=1569588865599",
             "https://sequenceimagestaging.blob.core.windows.net/retouch/s/400/5372009/five.jpg?v=1569588865599"
        ];
        var zip = new JSZip();
        var count = 0;
        var zipFilename = "Pictures.zip";  
        links.forEach(function (url, i) {
          var filename = links[i];
          filename = filename.replace(/[\/\*\|\:\<\>\?\"\\]/gi, '').replace("httpssequenceimagestaging.blob.core.windows.netretouch","");
          JSZipUtils.getBinaryContent(url, function (err, data) {
            if (err) {
              throw err;
            }
            zip.file(filename, data, { binary: true });
            count++;
            if (count == links.length) {
              zip.generateAsync({ type: 'blob' }).then(function (content) {
                FileSaver.saveAs(content, zipFilename);
              });
            }
          });
        });
    }

    render() { 
        return (  
            <div>
                <button onClick={this.download}>click me </button>
            </div>
        );
    }
}

export default ZipDemo;

我参考了jsfiddle

【问题讨论】:

    标签: reactjs download zip tsx


    【解决方案1】:
    let blob = fetch(url).then(r => r.blob());
        zip1.file(filename, blob, { binary: true });
    

    在

    的空间中添加这 2 行
    JSZipUtils.getBinaryContent(url, function (err, data) {
            if (err) {
              throw err;
            }
            zip.file(filename, data, { binary: true });
    

    它工作得很好,因为 jszip-utils 模块在 typescript 中不可用,我想我不确定,所以这是将文件转换为二进制文件的替代方法。

    【讨论】:

      【解决方案2】:

      JSZipUtils 似乎是一个单独的项目,也需要导入。试试https://www.npmjs.com/package/jszip-utils?

      【讨论】:

      • 找不到模块“jszip-utils”的声明文件。 '/home/tushar/Documents/html/my-app/node_modules/jszip-utils/lib/index.js' 隐含了一个 'any' 类型。尝试npm install @types/jszip-utils(如果存在)或添加包含declare module 'jszip-utils';的新声明(.d.ts)文件
      • 听起来您已将 TypeScript 配置为需要模块的类型定义。 jszip-utils 不提供类型定义,而且它似乎也没有在 DefinedTyped 上,所以你需要编写自己的或明确的任何类型的模块
      • 你能为此提出一些建议吗,我不知道......
      猜你喜欢
      • 1970-01-01
      • 2011-04-28
      • 2022-06-15
      • 2021-09-24
      • 2018-09-18
      • 2023-03-26
      • 2015-10-25
      • 2011-02-09
      • 1970-01-01
      相关资源
      最近更新 更多