下载二进制文件,而不会触发onbeforeunload的 [英] Download binary without triggering onbeforeunload
问题描述
我想揭开序幕文件下载,当他点击一个链接一个用户,但我有一个 onbeforeunload的
处理程序,我不希望被调用时,开始下载。要下载,我现在有一个< A>
与的href
设置为文件位置,但点击它导致 onbeforeunload的
在Chrome中被调用(而不是在FF,虽然)。
I want to kick off a file download for a user when he clicks a link, but I have an onbeforeunload
handler that I don't want to get invoked when the download begins. To downloads, I currently have an <a>
with the href
set to the file location but clicking it results in onbeforeunload
being invoked in Chrome (not in FF, though).
我知道我可以设置一个专用标志,并检查了 onbeforeunload的
的处理程序,但有一些方法使用AJAX揭开序幕下载?我还是希望用户看到常用的对话框时,下载文件(打开/保存等)。
I know I can set a private flag and check that in the onbeforeunload
handler, but is there some way to kick off the download using ajax? I still want the user to see the usual dialogs when they download the file (Open/Save etc).
想法?
推荐答案
要做到这一点确实是通过构建一个iframe,并从那里触发下载的最好方法。
The best way to do this is indeed by constructing an iframe and triggering the download from there.
我在Chrome,IE6 +和Firefox已经测试而这种做法似乎所有的人一起工作。
I have tested in Chrome, IE6+ and Firefox and this approach seems to work in all of them.
例如code:
function DownloadFile(filePath) {
var downloadIframe = $('<iframe />',
{
id : 'downloadIframe'
}).appendTo('body');
downloadIframe.attr('src',filePath');
}
这将只存储在一个更广泛的访问变正常了一次性的下载(因为我们已经很难codeD的ID),如果你是触发多次下载,那么我建议你重新使用的iframe
This will only work properly for a one off download (as we've hard coded an id), if you are triggering multiple downloads, then I suggest you reuse the iframe by storing it in a more widely accessible variable.
这篇关于下载二进制文件,而不会触发onbeforeunload的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!