Create and Send Zip file -NODE JS

可紊 提交于 2021-02-08 13:44:22

问题


I'm trying to create and then send zip file to client. I know how to create it but I've got a problem with send it to client. I tried many ways. I'm sending POST request from Client and as response I want to send a file. This is my server-site example code

var Zip = require('node-zip');
router.post('/generator', function(req, res, next) {
    var zip = new Zip;

    zip.file('hello.txt', 'Hello, World!');
    var options = {base64: false, compression:'DEFLATE'};
    fs.writeFile('test1.zip', zip.generate(options), 'binary', function (error) {
        console.log('wrote test1.zip', error);
    });
    res.setHeader('Content-disposition', 'attachment; filename=test1.zip');
    res.download('test1.zip');

}

}); I also tried something like this:

  res.setHeader('Content-disposition', 'attachment; filename=' + filename);
  res.setHeader('Content-type', mimetype);

  var filestream = fs.createReadStream(file);
  filestream.pipe(res);

I tried to use such libraries as:

  1. node-zip

  2. archiver

Can anyone explain me how to do that ?


回答1:


I haven't worked with node-zip or archiver before (I usually just use the built-in zlib module), but one thing I noticed right away is that you should place res.download inside the callback of writeFile. That way it will only send the file once it has been fully written to disk.

fs.writeFile('test1.zip', zip.generate(options), 'binary', function (error) {
    res.download('test1.zip');
});

I hope this solution works for you, if it doesn't feel free to comment.

Also, I think res.download sets the Content-disposition header for you, you don't need to set it manually. Not 100% sure on that one though.




回答2:


Try this express-easy-zip npm package to generate a zip file from a local folder path and send it as a download to the client.

var zip = require('express-easy-zip');
var app = require('express')();

app.use(zip());

app.get('my-route/zip', async function(req, res) {
    var dirPath = __dirname + "/uploads";
    await res.zip({
        files: [{
            path: dirPath,
            name: 'Package'
        }],
        filename: 'Package.zip'
    });
});



回答3:


Above solutions work.(above solutions generate zip and send it to frontend as data in response. In order to make it as downloadable following code will work) I was using express-zip. It is compressing files and sending data to frontend from backend(node). But in frontend I was getting only data in response. In my case I want user can be able to download the zip which sent by server. To solve this I followed following approach. For generating download window in browser i used downloadjs (we can follow another approach but i find this easy)

Front-End

const download = require('downloadjs')
return axios({
        url:process.env.API_HOST+'/getuploadedfiles',
        method:'get',
        headers:{
            'Content-Type': 'multipart/form-data',
            withCredentials:true,


        },
        responseType:'arraybuffer' // If we don't mention we can't get data in desired format
    })
    .then(async response => {
        console.log("got al files in api ");
        let blob = await new Blob([response.data], { type: 'application/zip' }) //It is optional

        download(response.data,"attachement.zip","application/zip") //this is third party it will prompt download window in browser.

        return response.data;
    })

Backe-End

const zip = require('express-zip');
app.use('/getuploadedfiles',function(req,res){
     res.zip([
    {path:'/path/to/file/file2.PNG',name:'bond.png'},
    {path:'/path/to/file/file1.PNG',name:'james.png'}
])



回答4:


This module works fine too: https://www.npmjs.com/package/adm-zip

Example without creating temporary zip file in server:

var AdmZip = require('adm-zip');
router.get('/zipFilesAndSend', function(req, res) {
    var zip = new AdmZip();
    // add local file
    zip.addLocalFile("./uploads/29/0046.xml");
    // get everything as a buffer
    var zipFileContents = zip.toBuffer();
    const fileName = 'uploads.zip';
    const fileType = 'application/zip';
    res.writeHead(200, {
        'Content-Disposition': `attachment; filename="${fileName}"`,
        'Content-Type': fileType,
      })
    return res.end(zipFileContents);
});


来源:https://stackoverflow.com/questions/33260789/create-and-send-zip-file-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!