0

I'm actually stuck with a problem of file download from my nodeJs server. I spent many times on other topics, without any success...

I have an app on the client that propose to download some text file. So I put this code for download process:

'click .downloadF': function(event) {
    var $target = $(event.target);
    var file = $target.model();

    var download = $.get('/files/download'+'?'+Math.random(),
                        {id:file.id});
  },

Up to here...very simple. On my nodeJS side I made that:

app.get('/files/download', function(req, res) {
var filePath = metrology.DownloadFile(req.query.id);

var filename = path.basename(filePath);
var mimetype = mime.lookup(filePath);

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

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

});

In the Response I get the right text data and the header is correctly set to attachement and file type. Response :

Connection keep-alive Content-Disposition attachment; filename=Metrology.csv Content-Type text/csv Date Mon, 16 Jun 2014 15:17:25 GMT Transfer-Encoding chunked X-Powered-By Express

Content:

NoPiece;NoRepetition;Mesure;Resultat; 1;1;1;3; 1;1;2;2; 1;1;3;2; 1;1;4;2; 2;1;1;2; 2;1;2;2; 2;1;3;2; 2;1;4;2;

Now... how can I open a dialog window to save these data into a specified filepath ???

I'm actually debuging on Mozilla, but have to work with IE9/11 too.

Many thanks for help, ideas, suggestions and (maybe) solutions :-)

  • Possible duplicate? http://stackoverflow.com/questions/22612009/open-save-as-dialog-box-to-download-image – bloodyKnuckles Jun 16 '14 at 15:29
  • Sorry but everywhere I saw the same information : need to define Content-Disposition to attachment... but in my case, I receive the data, but no dialog box is opened to save the file... Please HELP :-/ – user3745427 Jun 17 '14 at 10:21

1 Answers1

0

Is the jQuery .get function necessary? Does window.location.href accomplish your goal?

'click .downloadF': function(event) {
    var $target = $(event.target);
    var file = $target.model();

    window.location.href='/files/download'+'?'+Math.random()+'&id='+file.id;
  },
bloodyKnuckles
  • 11,551
  • 3
  • 29
  • 37