Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content-Disposition:attachment not triggering download dialog

Tags:

I've encountered some unexpected behavior when trying to create a file download functionality on my NodeJS server. I have a REST (express) API that calls for some export data function, which creates a CSV file on the server and uses res.download('path/to/file') to trigger the download. Response headers include

Content-Disposition:attachment; filename="indicators.csv"
Content-Length:30125
Content-Type:text/csv; charset=UTF-8

so everything seems to be in order.

The thing is, I get the response from the server as plain text. The response has all the data the CSV file contains, but does not trigger the browser's file download dialog like I intended. I tried both on Chrome and FF. The problem persists in both.

Any ideas?

Update

I managed to make it work by creating a dummy form, and using its submit action to make my AJAX call. But it's an ugly hack, and I'm still looking for a more elegant solution.

like image 443
Yaron Schwimmer Avatar asked Nov 04 '14 14:11

Yaron Schwimmer


People also ask

How do you use content disposition attachment?

In a regular HTTP response, the Content-Disposition response header is a header indicating if the content is expected to be displayed inline in the browser, that is, as a Web page or as part of a Web page, or as an attachment, that is downloaded and saved locally.

Is content disposition mandatory?

Content-Disposition is an optional header field.

What is content disposition attachment filename?

Content-Disposition is an optional header and allows the sender to indicate a default archival disposition; a filename. The optional "filename" parameter provides for this. This header field definition is based almost verbatim on Experimental RFC 1806 by R. Troost and S.

What is content disposition S3?

S3 provides multiple ways to set the Content-Disposition header of an object being downloaded, two of the main ways are: Set Content Disposition parameter on upload – works for new objects. Set response-content-disposition parameter in request – works for an existing object however requires a signed URL.


2 Answers

Headers are not the issue. The issue is that you are querying the download url via an ajax call, which will not invoke the browser download dialog. Your options boil down to the following:

  1. Use a form that is submitted to your download url. Instead of having a visible form a user has to interact with, create a form with JavaScript and submit it programmatically by calling form.submit - Handle file download from ajax post

  2. Point window.location to the download url. You can do this in the current window - download file using an ajax request , or in a new one - res.download() not working in my case

like image 191
Roman Pletnev Avatar answered Sep 17 '22 15:09

Roman Pletnev


You can try to use a different content-type, so that won't be open as a text file on the browser:

Content-Type:application/ms-excel; charset=UTF-8

Another alternative could be to use application/octet-stream as mime-type to define it as a downloadable file without a better description.

Content-Type:application/octet-stream; charset=UTF-8
like image 31
MQ87 Avatar answered Sep 17 '22 15:09

MQ87