rewrite URL to affect “save as” filename

谁说胖子不能爱 提交于 2019-12-11 15:49:09

问题


My application is in JSP/Struts and running on WebSphere. I don't have access to the server /really/ so I'm trying to do all this from within the app itself (Struts actions & config, JSP, etc).

I have a page that displays links for PDF documents that the user might want to look at. When they click on the link, it is forwarded to the 'document view' STRUTS action which performs the work to retrieve the PDF from the back-end system and then displays the PDF in the browser window. This all works fine.

The users have asked to have the file name of the document be customized for each document when they go to File->Save As... but right now the browser wants to try to name the document after the name of my Struts action. Example: "documentView.pdf"

I have the filename I want to use forwarded to the STRUTS action and I tried using the "Content-Disposition" header to set this, but haven't been able to get it to work. Actually it works great if I use "attachment; filename="myfilename" " but then it FORCES the "Save As..." dialog straight away, which I don't want either.

I want to be able to look at the PDF inline in the browser window and save it with a custom filename if I so choose.

I have read that this might be possible to do with URL rewriting but I'm having trouble sorting out how this technique might work.

Any ideas will be greatly appreciated.


回答1:


Change the "attachment" part of the Content-Disposition to "inline".

Browser support for Content-Disposition is flaky.

Before it was supported, you used to be able to get the browser to do the right thing by putting a superfluous key/value at the end of the URL. Like:

http://example.com/getPDF?id=123213&filename=mydoc.pdf

Some browsers picked up the last bit of the URL as the filename. Not sure if that still works.



来源:https://stackoverflow.com/questions/3569691/rewrite-url-to-affect-save-as-filename

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