如何在没有“离开页面”对话框或任何其他提示的情况下下载文件?

3
在Internet Explorer 10中,当我尝试下载文件时,会出现以下对话框:enter image description here。是否有一种在应用程序中用户尝试下载文件时不会出现此对话框的JQuery或HTML方式?
更新: 由于客户要求不能出现新标签闪烁,因此也不能使用target_blank。 如要求提供代码,代码很简单,只是一个链接。
<a href="www.something.com/something/something.pdf" >

你应该在 _blank 窗口中打开下载链接。 - Parth Trivedi
你的下载按钮/链接是什么?请在这里编写代码。 - Parth Trivedi
@Parth Trivedi 不,我不想要那个;它会闪烁一个不必要的新标签页。还有其他办法吗? - fatherazrael
1
just use <a href="www.something.com/something/something.pdf" target="_blank"> - Parth Trivedi
1
只需尝试访问 https://jqueryfiledownload.apphb.com/ 并在IE浏览器中打开即可。 - Parth Trivedi
显示剩余8条评论
2个回答

1
"

jquery.fileDownload.js可以帮助你实现这个目标。

将文件路径应用于:

"
function downloadPDF(thisobj) {        
    $.fileDownload($(thisobj).attr("href"));
}

在此调用该函数

<a href="www.something.com/something/something.pdf" onclick="downloadPDF(this)"> 

我已经尝试过了。请查看这个fiddle -> https://jsfiddle.net/kxzq4ep8/; 它的运行结果与预期不符。在IE 10中,它显示了不必要的对话框,在Firefox中则显示“阻止加载混合活动内容”的消息:“http://www.gasl.org/refbib/Bible_King_James_Version.pdf”。 - fatherazrael
这是由于跨域请求引起的。因为fileDownload Js使用ajax来处理文件请求失败和请求。 - Parth Trivedi
如果您的代码在同一域中,那么就没问题了。否则,请使用window.location.hostname检查跨域,并在新窗口中打开外部链接。 - Parth Trivedi
目前看起来还不错。给它一个赞。让我部署并测试一下在IE 10/11、Chrome、Safari和Firefox上的表现。然后就能得出答案了。 - fatherazrael

1
我建议您了解HTML5的下载属性
网站摘录:

Place the download attribute on a link...

<!-- will download as "expenses.pdf" -->
<a href="/files/adlafjlxjewfasd89asd8f.pdf" download="expenses.pdf">Download Your Expense Report</a>

...and when the user clicks the link, the download attribute appears in the save dialog instead of the garbled mess that was there before. In this case, the file will be downloaded as expenses.pdf. The download attribute also triggers a force download, something that I used to do on the server side with PHP.


它为什么不能用?简单地说“不能用”,然后就对答案进行贬低是没有帮助任何人的。 - Alexis Tyler
我没有对答案进行负评。它真的不起作用。我实施了相同的方法后仍然会弹出对话框。在我完全确信之前,我不会投票支持任何东西。 - fatherazrael
在Firefox上,它首先显示离开页面、留在页面的对话框,然后再显示“另存为”对话框。其他浏览器也是如此。 - fatherazrael

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接