Chrome adds “-, attachment” to downloaded file

大憨熊 提交于 2019-12-10 12:56:14

问题


I use Symfony2 Framework and use the following code to export an xml file:

$response->setStatusCode(200);
$response->headers->set('Content-Type', 'application/xml');
$response->headers->set('Content-Description', 'Submissions Export');
$response->headers->set('Content-Disposition', 'attachment; filename="' . $filename .'"');
$response->headers->set('Content-Transfer-Encoding', 'binary');
$response->headers->set('Pragma', 'no-cache');
$response->headers->set('Expires', '0');

I does not matter whether I take "Content-Transfer-Encoding" or "Pragma" or all of them except "Content-Type" and "Content-Disposition" away. The result in Chrome is always: "filename-, attachment" (without the "). In Firefox this works fine.

So for instance, if I have a file called home.xml Firefox will download home.xml, whereas Chrome will give me home.xml-, attachment.


回答1:


I know this is a pretty old discussion, but if anyone gets here looking for help with this, I determined that chrome seems to want a semicolon at the end of the filename and then it's happy. As in (VBScript):

Response.AddHeader "Content-Disposition", "attachment;filename=Something.xls;"

That fixed it for me. -Dave




回答2:


I had the same problem in ZEND because I found a lot of non working stuff I wanted to add my anwser to this post. Since it took me a while to solve it thought more users and developers would wanted to know how I solved it. I add this just before my fopen call.

header('Content-Disposition: attachment; filename='.time().'.csv');

Solved the issue for me. Before I used a contextswitched what was buggy but now it works as a charm. Do not forget to remove previous content disposition headers hence you might get a double header error.



来源:https://stackoverflow.com/questions/19834828/chrome-adds-attachment-to-downloaded-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!