Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force a browser to NOT download a file with content-disposition:attachment header

For example:

<iframe src="http://otherdomainidontcontrol.com/blah.csv"></iframe>

And blah.csv has this header:

Content-Disposition: attachment; filename=blah.csv;

Is it possible to force blah.csv to render in the iframe instead of downloading?

like image 782
Matt York Avatar asked Aug 07 '12 16:08

Matt York


People also ask

How do I force files to open in the browser instead of downloading PDF?

The correct type is application/pdf for PDF, not application/force-download .

Is content-disposition required?

Content-Disposition is an optional header and allows the sender to indicate a default archival disposition; a filename.

What does content-disposition header do?

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.


1 Answers

We've actually been looking into this for our application, since we want the browser to render the file embedded if it is capable of doing so - and if not capable, we want the browser to download the file under an appropriate name.

If you change the Content-Disposition header to be inline instead of attachment, it works in that manner - the browser will render the file if it is able to do so, and if not, the file will be downloaded as whatever you specify in the filename portion of the Content-Disposition header

response.headers['Content-Disposition'] = "inline; filename=name.extension"

However, if as you have said, your blah.csv comes back with that header and you can't intercept it or change that, then I would agree there is no way around it. The "attachment" part specifies that the file be downloaded.

like image 110
Krista Avatar answered Sep 30 '22 03:09

Krista