0

I want to prompt a download for a user when they click a link instead of open it in the browser.

with expressJS, it should be just like this :

app.get('/download', function (req, res) {
  res.download('public/uploads/sample.pdf');
});

how can I do it with GeddyJS ?

I tried this code, but it still open it in the browser, which is mean the "resp.setHeader" is not working.

this.download = function (req, resp, params) {
  var filename = 'sample.pdf';
  var file = 'public/uploads/sample.pdf';

  resp.setHeader('Content-Disposition', 'attachment; filename="' + filename + '"');
  resp.sendFile(file);
};

Did I miss something?

Thanks a lot before.

fin
  • 519
  • 4
  • 7

1 Answers1

3

The issue is that Geddy's response object doesn't have a 'setHeader' method, so you need to reach in and set the header on the real Node response object.

resp.resp.setHeader('Content-Disposition', 'attachment; filename="' + filename + '"');
loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
  • still not working because the respond header is always: " Connection:keep-alive Content-Type:image/png Date:Fri, 05 Apr 2013 10:59:14 GMT Transfer-Encoding:chunked", no matter what string I placed with the resp.setHeader(). Is that possible that resp.sendFile() overrule the setHeader? If yes, then how to set the header? – fin Apr 05 '13 at 10:59
  • @fin Just to confirm, is your example with a different file? You said `image/png` but your question is about a PDF. I think I know why it didn't work, but I just want to confirm first. – loganfsmyth Apr 07 '13 at 05:36
  • ah sorry I mean "Connection:keep-alive Content-Type:application/pdf Date:Fri, 05 Apr 2013 10:59:14 GMT Transfer-Encoding:chunked". I was also trying to download an image, and still not working. – fin Apr 09 '13 at 11:13
  • @fin Updated, let me know how that works. Geddy does some weird stuff with mixins, so the outer `resp` has a `setHeader` function, but I don't think it actually does anything. – loganfsmyth Apr 09 '13 at 17:05
  • nice.. working.. didn't know that `resp` also have `resp`.. thx man... at least it is fixed in this version. – fin Apr 11 '13 at 19:01