Node express content-order

I want to force the browser to download a file from external storage given the url. I implemented this express action after closing the controller:

var download = function(req, res) {
    request(req.body.url).on('response', function(response) {
        res.set({
            'Content-Disposition': 'attachment; filename=' + req.body.filename,
            'Content-Type': response.headers['content-type']
        });
    })
    .pipe(res);
};

      

I don't know why the browser always gets "inline" instead of "attachment", avoiding the file download.

In this case I am using express and request, the server is hosted on Heroku and the server that contains the files is FilePicker.

+3


source to share


1 answer


I'm looking around for the same thing. The express documentation shows a very simple helper method right out of the box:



var express = require('express');
var app = express();

app.get('/download', function(req, res) {
  res.download('/path/to/your_file.pdf');
});

      

+3


source







All Articles