11

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.???

codeofnode
  • 18,169
  • 29
  • 85
  • 142

2 Answers2

31

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');
robertklep
  • 198,204
  • 35
  • 394
  • 381
  • also if i remove the '/' in the path name.. i get 404 – codeofnode Nov 24 '13 at 16:41
  • yes i have removed the headers and result is same and still i am not getting the expected result. browser should ask to save file on disk, – codeofnode Nov 24 '13 at 16:43
  • @Koka which browser? It works fine in Chrome, FF and Safari for me. Also, are you opening `/download` directly, or through something like AJAX? – robertklep Nov 24 '13 at 16:43
  • i means with the same.. i have added he res.get('/download' ... line but result is same.. getting the response filled with file content. – codeofnode Nov 24 '13 at 16:46
  • i am using firefox on ubuntu 12,04 – codeofnode Nov 24 '13 at 16:47
  • demo from http://runnable.com/UpIbxCTySek9AAA2/output gives me exact result.. but not from the code on my local machine – codeofnode Nov 24 '13 at 16:48
  • @Koka I don't think AJAX calls trigger a download action in any browser, so that's not going to work. If you want to present the user with a download dialog, you should probably use `window.open()`. – robertklep Nov 24 '13 at 16:50
  • can i send some query parameters (an array of values) dynamically with the window.open()..?? – codeofnode Nov 24 '13 at 17:00
  • thanks for your answer.. but how do i send a parameter of array of values,.. not just two static parameters – codeofnode Nov 24 '13 at 17:10
  • 1
    @robertklep thanks window.open worked for me. But i am facing a new issue. My service sends a zip file as response. But in my case only an empty zip file is downloaded – Apoorva sahay Aug 02 '16 at 20:21
  • @Apoorvasahay if you create a new question (including the code you're using to create and send back the ZIP file), I'll have a look if you want. – robertklep Aug 02 '16 at 20:42
  • @robertklep here is the new question http://stackoverflow.com/questions/38733278/why-res-download-is-downloading-an-empty-zip-file – Apoorva sahay Aug 03 '16 at 02:15
10

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.

moolsbytheway
  • 1,152
  • 13
  • 20