I am using nodejs and expressjs framework to download a file 'jsonFile.json' from server.
i am using the following code
res.get('/download', function(req, res) {
res.setHeader('Content-disposition', 'attachment; filename=jsonFile.json');
res.setHeader('Content-Type', 'text/json');
res.download(__dirname + 'jsonFile.json');
}
});
But this results into a response with whole content of file.
i was expecting browser to ask me to save the file in local disk.
How do i save the file in local disk.???
Just to confirm what @robert said,
because this thing turned my head for two days, instead of using an ajax call, open a new window with your ajax request location, for example:
window.open("http://yourserver.com/api/link?a=3&b=4")
hope this helps someone.
Let Express set the correct headers and just do this:
res.get('/download', function(req, res) {
res.download(__dirname + 'jsonFile.json', 'jsonFile.json');
});
(doc)
EDIT: since you're requesting /download
through an AJAX call, you have to change your setup because most (all?) browsers will not show a download dialog in that case.
Instead, you can create a new window from your front end code to trigger the dialog:
window.open('/download?foo=bar&xxx=yyy');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With