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:
Accept-Ranges:
bytes
Cache-Control:
public, max-age=0
Connection:
keep-alive
Content-Disposition:
attachment; filename="report1362670336342.pdf"
Content-Length:
30042
Content-Type:
application/pdf
Date:
Thu, 07 Mar 2013 15:32:16 GMT
ETag:
"30042-1362670171000"
Last-Modified:
Thu, 07 Mar 2013 15:29:31 GMT
Vary:
Accept-Encoding
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