You can, but there is more work. When the ajax response comes down you get the data but the browser doesn't automatically try to display it (which is what causes download boxes to pop up). Instead you can convert it to a dataURL and then do something like:
window.location = myDataUrl; Which will being up a download prompt; this method isn't without it's own problems though too. -Chad From: [email protected] [mailto:[email protected]] On Behalf Of Geoff Bell Sent: Thursday, March 07, 2013 3:18 PM To: [email protected] Subject: [nodejs] Re: Client file download via req.download Guess you can't do download via ajax. If I use a <form>, it works just fine. Lesson learned! On Thursday, March 7, 2013 11:04:08 AM UTC-5, Geoff Bell wrote: I'm trying to add a download button to our site. Clicking it initiates this ajax request: $.ajax url: '/reports/get_file_from_json_service' data: { docId:window.DOCUMENT_ID, file_type:'pdf' } type: 'POST' success: (data, stat, xhr) => console.log 'download', stat error: (xhr, stat, err) => console.log 'download', stat, err This hits some service that retrieves the current document from a data-store. For testing in an offline environment, I've mocked an Express route and ends up here: exports.getPDF = (req, res) -> date = new Date().getTime() console.log "Downloading document #{req.body.docId} of type #{req.body.file_type}" res.download 'mock_routes/report.pdf', "report#{date}.pdf", (err) -> if err console.log "something went wrong downloading report-#{date}.pdf", err else console.log 'success' exec 'touch mock_routes/report.pdf' This appears to be working. I get a success message from the server, and from the client. I can see the PDF body using Chrome's Inspector. However, the browser is not prompting me to save the PDF. The response header looks like so: 1. Accept-Ranges: bytes 2. Cache-Control: public, max-age=0 3. Connection: keep-alive 4. Content-Disposition: attachment; filename="report1362670336342.pdf" 5. Content-Length: 30042 6. Content-Type: application/pdf 7. Date: Thu, 07 Mar 2013 15:32:16 GMT 8. ETag: "30042-1362670171000" 9. Last-Modified: Thu, 07 Mar 2013 15:29:31 GMT 10. Vary: Accept-Encoding 11. X-Powered-By: Express My understanding is that the Content-Disposition tells the browser to save the file, and suggest the filename "report{date}.pdf. This isn't happening though. Nothing happens. Any ideas why the browser is not prompting to Save As? Thank you, Geoff -- -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out. -- -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
