how to prompt where to download zip file created with archiver in node

732 views Asked by At

I am trying to create a zip file in node using the code provided from how to create a zip file in node given multiple downloadable links, as shown below:

var fs = require('fs');
var archiver = require('archiver');
var output = fs.createWriteStream('./example.zip');
var archive = archiver('zip', {
    gzip: true,
    zlib: { level: 9 } // Sets the compression level.
});

archive.on('error', function(err) {
  throw err;
});

// pipe archive data to the output file
archive.pipe(output);

// append files
archive.file('/path/to/file0.txt', {name: 'file0-or-change-this-whatever.txt'});
archive.file('/path/to/README.md', {name: 'foobar.md'});

//
archive.finalize();

When I use this suggestion, the zip file is downloaded without any kind of prompt asking me where I would like to save the file - is there any way I can make it so that a prompt is created asking me where I would like to save the file, which is quite normal these days?

If this is absolutely not possible, would it be possible to always save the file in the downloads folder (regardless of whether on mac or windows or any other operating system)?

1

There are 1 answers

1
razki On

So there's a couple of things here. In terms of a 'prompt' or 'pop-up' you won't find anything along the lines of WinForms out of the box, there are options for the command line such as prompts You can use that as your user input.

https://www.npmjs.com/package/prompts

You'll want to use path and more specifically path.join() to combat the mac/windows/linux issue.

Do you need to use path.join in node.js?

You can run an express server and create a route that uses res.download() in which you would provide the zipped file.

https://expressjs.com/en/api.html#res.download