如何强制下载文件提示,而不是使用HTML在浏览器中显示它? [英] How to force a Download File prompt instead of displaying it in-browser with HTML?

查看:254
本文介绍了如何强制下载文件提示,而不是使用HTML在浏览器中显示它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < a href =sample.pdftarget =_ blank>下载< / a> 

如果我点击下载按钮,这个目标空白将打开一个新窗口。



但我需要它提示保存此文件的对话框。我怎样才能做到这一点?

解决方案

这是你无法完全控制HTML本身的东西。



如果用户使用带有PDF阅读功能的浏览器插件)和相应的设置打开浏览器中的PDF文件,PDF将以这种方式打开。



由于您的<$ c

如果您使用的是HTML5,则可以使用下载属性

 < a href =sample.pdfdownload =sample.pdf>下载< / a> 

如果您有一个您可以控制的后端服务,或者您觉得像摆弄Web服务器,你总是可以寻找设置正确的 Content-Disposition 。看到这个问题的关于内容处置


 <a href="sample.pdf" target="_blank">Download</a>

If I click Download button, this target blank is opening a new window.

But I need it to prompt a dialog for saving this file. How can I achieve this?

解决方案

This is something that you cannot absolutely control with HTML itself.

If the user is having a browser with PDF reading capabilities (or a plugin) and the corresponding settings to open PDF files in-browser, the PDF will open like that.

The PDF opens in a new tab simple because of your target="_blank", which has nothing to do with a download prompt.

If you are using HTML5 you can use the download attribute:

<a href="sample.pdf" download="sample.pdf">Download</a>

If you have a back-end service which you can control or you feel like fiddling with your Web Server, you can always look for setting the right Content-Disposition. See this SO question for some nice discussion on Content-Disposition.

这篇关于如何强制下载文件提示,而不是使用HTML在浏览器中显示它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆