Blob createObjectURL下载在Firefox中无法使用(但在调试时工作) [英] Blob createObjectURL download not working in Firefox (but works when debugging)

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

问题描述

我有一个奇怪的问题,下面的功能是基于我在网上发现的,在客户端中快速创建一个Blob(一个数组传递)并且能够下载的一个二进制数据创建的那。这在Chrome中非常出色,但在Firefox中没有任何作用 - 除非我调试并逐步执行代码。是的,奇怪的是,如果我在函数内创建一个断点,并通过它,a.click()将弹出下载窗口!

  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进行测试。

解决方案

您可能已经很快删除资源,尝试延迟它

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


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);    
}

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天全站免登陆