Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make Safari download a CSV instead of showing it in the browser?

In Safari, my CSV files are being shown in the browser instead of downloading them using the Save As window.

I tried: response.setContentType("text/csv; charset=UTF-8");. The other browsers are fine with the second option, but Safari kept displaying the file in-browser instead of prompting the user to save it.

By the way, i'm running Liferay.

I fixed it by including this: response.setContentType("application/csv; charset=UTF-8");

Thanks.

like image 747
user1916494 Avatar asked Oct 05 '22 07:10

user1916494


2 Answers

text/csv would be the most appropriate for the content type. You also want to set the content-disposition like this:

response.setHeader( "Content-Disposition", "attachment;filename=" + filename );
like image 135
Jeffrey Ray Avatar answered Oct 20 '22 00:10

Jeffrey Ray


I think you'll need to set the Content-Disposition HTTP header as well.

The HTTP header should look like this:

Content-Disposition: attachment; filename=WHATEVER_FILENAME_YOU_WANT

Does the response object have a setContentDisposition method too?

like image 2
Paul D. Waite Avatar answered Oct 20 '22 00:10

Paul D. Waite