1

I'm trying to have a controller action in my app, upon a button being clicked by a user, attach the requested file and redirect all in one controller action.

I'm having trouble doing so though, because if I place a res.redirect after my res.attachment() ; res.send(), the file won't send, and if I leave out the redirect, the file sends but no redirect occurs. How should I handle this?

kk415kk
  • 1,227
  • 1
  • 14
  • 30
  • It's not clear what your desired outcome is. `res.attachment` will attach a `content-disposition` header so that when the response is sent, a download dialog will pop up in the browser to download the file. There's no redirecting after that. – sgress454 Jun 02 '14 at 18:35
  • I guess my goal is for the following series of actions to occur: 1. User clicks on "download" 2. User is redirected to a new page and the download dialog appears – kk415kk Jun 02 '14 at 18:46

1 Answers1

1

Based on your stated goal, this isn't something you would handle on the back end. Instead, just have your download button redirect to the new page, and add code to that page to automatically start the download. Lots of suggestions for how to auto-start the download can be found in this SO question.

Community
  • 1
  • 1
sgress454
  • 24,870
  • 4
  • 74
  • 92