0

I use a library that accepts a file as one of its parameters and it uses something like

<input type="file" id="file" name="files"/> 

to get the needed file. Now, what i want is to send the file from my node.js server to the client via rest API like this:

$.get ('/getfile', function(data) {
    reader = new FileReader();
    reader.onload = function(theFile){
        //some code here
    };
    reader.readAsText(f); 
});

However, if i use something like this on the server side:

app.get('/getfile', function (req, res) {
    var pathx = 'path to file';
    res.download(pathx);
}); 

when it reaches the client, it does not see it as a file, rather the variable data, contains the contents of the file. how can i send a file down to the client so that the client can still see it as a file.

overmind
  • 467
  • 1
  • 8
  • 17
  • Try to set response header "Content disposition" : http://stackoverflow.com/questions/20508788/do-i-need-content-type-application-octet-stream-for-file-download – Timmy Aug 23 '15 at 10:01
  • Hi, It still outputs the contents of the file. Here is my code: `app.get('/getfile', function (req, res) { var pathx = 'path_to_file'; res.setHeader('Content-type', 'application/octet-stream'); res.setHeader('Content-Disposition', 'attachment; filename=file1'); res.download(pathx); });` – overmind Aug 23 '15 at 12:15

1 Answers1

0

I can't post comment, so need to write an answer. $.get call is a part of your client side code, right? And you want to download file using jQuery.

Then there are 2 parts. 1. You need to have method on the server that serves file correctly (headers set up, etc). Open your browser and go to that address. What should happen is your browser downloads file. If it doesn't, you need to resolve this issue first.

  1. Then you'd make your client make some calls using jQuery to download file. How to do this you can find in other answers like Download File Using jQuery

Now, can you say whether you have successfully completed step 1?

Community
  • 1
  • 1
Timmy
  • 251
  • 3
  • 12