【问题标题】:how to send a zip file back end to front end in nodejs using express server如何使用快速服务器在nodejs中将zip文件后端发送到前端
【发布时间】:2020-04-22 02:50:17
【问题描述】:

我正在构建一个应用程序,它需要将数据保存到单独的文件中并将这些文件压缩成一个 zip 文件。这些文件位于后端,我无法将此后端创建的 zip 文件发送到前端,在这种情况下会做出反应

表达js代码

app.post("/download",function(request,response,next){
    let sheets=request.body["sheets"];
    let charts=request.body["charts"];
    let compressedSheets=LZString.compress(JSON.stringify(sheets));

    fs.writeFile(__dirname+'/public/dataModel.txt', compressedSheets, function (err) {
        if (err) throw err;
        console.log('Replaced!');

      },()=>{
        fs.writeFile(__dirname+'/public/Report.json',charts,function(err){
            if(err){
                console.log(err);
            }
        },()=>{
            var archiver = require('archiver');
            var output = fs.createWriteStream(__dirname+'/public/example.zip');
            var archive = archiver('zip', {
                gzip: true,
                zlib: { level: 9 } // Sets the compression level.
            });

            archive.on('error', function(err) {
            throw err;
            });
            archive.pipe(output);
            archive.file(__dirname+'/public/dataModel.txt', {name: 'dataModel.txt'});
            archive.file(__dirname+'/public/Report.json', {name: 'Report.json'});


            archive.finalize().then(()=>{
                response.setHeader('Content-disposition', 'attachment; filename=example.zip');
                response.download(__dirname+'/public/example.zip');
            });

        })
      });

反应代码

handleSaveAs=function(){


    let data=new FormData();
    data.append('sheets',JSON.stringify(this.state.sheets));
    data.append('charts',JSON.stringify(this.state.charts));


    axios
    .post("http://localhost:4001/download",data)
    .then(res=>{
      console.log(res);
      const element = document.createElement("a");
      const file = new Blob([res.data], {type: 'application/zip'});
      element.href = URL.createObjectURL(file);
      element.download = "untitled.zip";
      document.body.appendChild(element);
      element.click();
    })

只要正确处理所有导入并在后端正确创建 zip 文件。问题仅在于将其发送到前端

任何帮助将不胜感激 谢谢

【问题讨论】:

    标签: javascript reactjs express zip


    【解决方案1】:

    您可以使用 Node.js 内置的fs 将数据流式传输到前端。

    //Filestream middleware that takes in the file path as the parameter
    
    const streamW = (pathToZip) => {
     return (req, res, next) => {
    
    //Create a readable stream
    const readableStream = fs.createReadStream(pathToZip, 'the_encoding_here');
    
    //Pipe it into the HTTP response
    readableStream.pipe(res)
    
      next();
    
    }};
    
    //The route that you want to hit using the front-end to get the file
    //Call the middleware and pass in the path to the zip
    router.get('/downloadRoute', streamW('path_to_zip'), (req, res) => {
    
         //Send the data to the front end by calling res
         res
    
    // });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-06
      • 2023-03-06
      • 2020-02-28
      • 2020-05-23
      • 1970-01-01
      • 1970-01-01
      • 2021-06-12
      • 2021-09-09
      相关资源
      最近更新 更多