Blob createObjectURL 下载在 Firefox 中不起作用(但在调试时有效) [英] Blob createObjectURL download not working in Firefox (but works when debugging)

查看:36
本文介绍了Blob createObjectURL 下载在 Firefox 中不起作用(但在调试时有效)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,下面的函数是我根据我在网上找到的关于使用一些二进制数据(作为数组传递)在客户端动态创建一个 Blob 并能够下载的内容创建的那.这在 Chrome 中非常有效,但在 Firefox 中不起作用 - 除非我调试并逐步执行代码.是的,奇怪的是,如果我在函数内部创建一个断点并单步执行,a.click() 将打开下载窗口!

I have an odd problem, the function below is one I created based on what i found on the net about creating a Blob in the client on the fly with some binary data in (passed as an array) and being able to download that. This works brilliantly in Chrome, but doesn't do anything in Firefox - UNLESS I debug and step through the code. Yes, oddly, if I create a break point inside the function and step through it, the a.click() will bring up the download window!

function downloadFile(filename, data) {

    var a = document.createElement('a');
    a.style = "display: none";  
    var blob = new Blob(data, {type: "application/octet-stream"});
    var url = window.URL.createObjectURL(blob);
    a.href = url;
    a.download = filename;
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
    window.URL.revokeObjectURL(url);    
}

谁能帮帮我?这是使用 Firefox 38.0.5 测试的.

Can anyone help me? This was tested using Firefox 38.0.5.

推荐答案

您可能过早地移除资源,请尝试延迟它

You're probably removing the resource too soon, try delaying it

    ...
    a.click();
    setTimeout(function(){
        document.body.removeChild(a);
        window.URL.revokeObjectURL(url);  
    }, 100);  
}

这篇关于Blob createObjectURL 下载在 Firefox 中不起作用(但在调试时有效)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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