How can I force a “save as” option when the user clicks a link

笑着哭i 提交于 2019-12-11 00:54:00

问题


I have a link to a PDF on my web page and I want to know if it's possible to always ask the user if they want to download the PDF or open it in a new window. Is this possible? I realise that people would not want the PDF to automatically download, but 2 links or an option would be great.

There is quite a lot of documentation about this on the internet, some even saying I'd have to have PHP and this is not possible on the server I use.

I need a javascript, jQuery solution or alternative suggestion. Not asking for much I know.

Thanks very much for reading this and I hope it makes sense.

All the best! T.


回答1:


It can be done, but not in clientside javascript. If you have a server side script that feeds the file, it can send a Content-Disposition: attachment header along with the response. This will trigger a save as dialog in the browser.

If they are static files, you can also change the configuration of your server. For Apache use the FilesMatch directive. There's a nice example for PDF files here:

http://www.thingy-ma-jig.co.uk/comment/7045



来源:https://stackoverflow.com/questions/8429505/how-can-i-force-a-save-as-option-when-the-user-clicks-a-link

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