Transfer files from server to client ?? [closed]

1

I am generating a PDF on the NodeJS server and when it is ready I want it to be sent to the client and downloaded, all through a GET request. The file will be generated on the server, stored in a specific address and then returned in the GET request, or it can also be sent by clicking on a link. All I need is to figure out how to send this PDF to the client.

    
asked by anonymous 02.08.2017 / 15:07

1 answer

1

Assuming you are using the express server, use res.download

It downloads the file in the path as a attachment :

var express = require('express');
var router = express.Router();    

router.get('/download', function (req, res, next) {
    var filePath = "/Users/meuuser/PDFs"; //caminho do arquivo completo
    var fileName = "report.pdf"; // O nome padrão que o browser vai usar pra fazer download

    res.download(filePath, fileName);    
});

Note: There is res.attachment , but if you use it, it will set the header Content-Disposition to "attachment" and this will make the browser understand that it should be seen as an attachment, not a webpage When you use res.download , it transfers the file as an attachment, then usually browsers will consider downloading the file.

Reference: Express documentation res.download .

    
04.08.2017 / 21:34