PDF Handler : content-disposition filename

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 01:58:04

问题


I am outputting a PDF file in a Web browser (IE8) HttpContext.Response.writefile(fileName) and it works great. When I try to save the file, it will give me the name of the ashx handler as a default. I would like to actually pass the real name.

I tried to add header information as follow:

context.Response.AddHeader("content-disposition", "attachment; filename=" + fileInfo.Name);

And it works but I do not want the user to have to choose between open and save, i want the file to open normally and if the user chooses to save it then the dialog would give him/her the default filename.

I tried also:

context.Response.AddHeader("content-disposition", "inline; filename=" + fileInfo.Name);

Or just, like Scott Hanselman suggested in his blog.

context.Response.AddHeader("content-disposition", "filename=" + fileInfo.Name);

None of those work for me. Does anybody have any ideas?


回答1:


See test cases at http://greenbytes.de/tech/tc2231/#inlwithasciifilenamepdf - it seems that this is simply a missing feature in IE.




回答2:


I also came across this problem. What helped me was to also set the contenttype to application/pdf (instead of application/x-pdf, which is outdated)

response.setContentType("application/pdf");
response.setHeader("Content-disposition", "inline; filename=\"Report.pdf\"");



回答3:


In case of INLINE, it seems that Internet explorer is using the last parameter in the URL to build the filename. For example if your url is http://doc.com/131231231 IE will prompt you to save the file as 131231231.pdf If you need a filename for example foo_131231231.pdf you can hack the IE by using this url: http://doc.com/foo_131231231 You may suffer to change your app a bit to expect such ugly parameter, but at the end your app will work as you expect.



来源:https://stackoverflow.com/questions/14818732/pdf-handler-content-disposition-filename

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