user5237222
user5237222

Reputation: 31

Node JS/Express render html and then download a file same GET request

I would like a landing page sent via email link and use same GET to attach a file download.

I would like to both render a Handlebar template using Express Static, and use same Req flow to download a file. I know requests cannot be set twice, so how can I get around this using one GET??? The download may take a few moments to prepare before downloading- so a landing page is need after user clicks the email link- but I need the request for the download file too.

Thanks in advance for any ideas you may have. Stuck on this one.

For example:

app.get('/download a file from email link', function (req, res, next) {

res.render('index') // replies w Template rendered Express...

Then after header is set, download a file after the header has been replied to in same GET:

res.attachment('file');
res.send(data for download);

Upvotes: 1

Views: 2652

Answers (1)

Bastien Adam
Bastien Adam

Reputation: 73

I think that is not possible to send page and file in same time, but any solution exists:

  • Using an iframe in the page to download the file from the server : <iframe style="display:none;" src="http://URL_OF_FILE_TO_DOWNLOAD"></iframe> (not using one GET :( )

  • User meta refresh to redirect to the file to download : <meta http-equiv="refresh" content="0; URL=http://URL_OF_FILE_TO_DOWNLOAD"> (using one GET, but twice request* )

  • Or, with a hack, you can send the file in the html, then save it in client side with data URIs see here (work for text file, but I don't know for other file types) (using one GET !)

*With meta redirect method, if you absolutly want to download from the same URL, use referer to determin origin after the redirection with req.headers.referer, then if it's equal to your page, send the file, else, send your page


It's my first answer on this website

Upvotes: 1

Related Questions