简体   繁体   English

如何让Safari下载CSV而不是在浏览器中显示?

[英]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. 在Safari中,我的CSV文件显示在浏览器中,而不是使用“另存为”窗口下载它们。

I tried: response.setContentType("text/csv; charset=UTF-8"); 我试过: 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. 其他浏览器可以使用第二个选项,但Safari会继续在浏览器中显示该文件,而不是提示用户保存它。

By the way, i'm running Liferay. 顺便说一句,我正在运行Liferay。

I fixed it by including this: response.setContentType("application/csv; charset=UTF-8"); 我通过包含这个来修复它:response.setContentType(“application / csv; charset = UTF-8”);

Thanks. 谢谢。

text/csv would be the most appropriate for the content type. text/csv最适合内容类型。 You also want to set the content-disposition like this: 您还希望像这样设置内容处置:

response.setHeader( "Content-Disposition", "attachment;filename=" + filename );

I think you'll need to set the Content-Disposition HTTP header as well . 我认为您还需要设置Content-Disposition HTTP标头

The HTTP header should look like this: HTTP标头应如下所示:

Content-Disposition: attachment; filename=WHATEVER_FILENAME_YOU_WANT

Does the response object have a setContentDisposition method too? response对象是否也有setContentDisposition方法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM