Re: Client download of file via res.download() not working

1,396 views
Skip to first unread message

Geoff Bell

unread,
Mar 7, 2013, 3:17:21 PM3/7/13
to expre...@googlegroups.com
Guess you can't do download via ajax.  If I use a <form>, it works just fine.  Lesson learned!

On Thursday, March 7, 2013 12:30:40 PM UTC-5, Geoff Bell wrote:
I posted this question in the nodejs group, but realized that I should have posted it here.  Sorry for the cross posting.

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
Reply all
Reply to author
Forward
0 new messages