1

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);
Zombo
  • 1
  • 62
  • 391
  • 407

1 Answers1

1

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

Community
  • 1
  • 1
  • Thanks you for your reply- great suggestions, but unfortunately to save bandwidth and Heroku cost I would like to get file from AWS S3 and pass the 'chunks' to the user for a file download similar to Express: res.attachment('file') and send. I dont believe I can do this w iFrame, but I thought I would ask- do you know of anyway to attach the 'Chunks' from a data.body stream into an iFrame??? Thanks, again for the reply – user5237222 Aug 18 '15 at 16:15
  • I think that is only possible for text based files or images to be converted to a link to put it in a iFrame or other things like it (I havent find other files type at search for data URIs). But you must know that the generated link will be bigger that the file, and I think that the page will be not sent before it's totaly generated. If you have a text file, you can copy it: http://jsfiddle.net/onfbj2o9/1/ . Else I can't help you. For saving bandwith, if you had a big website, you can see for p2p like peer5. Sorry for my bad english – Bastien Adam Aug 20 '15 at 13:25