Transfer files from the server to the client?

Asked

Viewed 470 times

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 this through a GET request. The file will be generated on the server, stored at a specific address and then returned in the GET request, or can also be sent via a click on a link. All I need is to figure out how to send this PDF to the client.

1 answer

1


Assuming you’re using the express server, use the res.download

It transfers the file on the way 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 the res.attachment, but if Voce uses it, it will set the header Content-Disposition as "Attachment" and this will make the browser understand that it should be viewed as an attachment, not a webpage. When you use res.download, he transfers the file as attachment, so usually browsers will consider downloading the file.

Reference: Express Documentation res download..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.