我有一个奇怪的问题,下面的函数是我根据我在网上找到的关于使用一些二进制数据(作为数组传递)在客户端动态创建一个 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 中不起作用(但在调试时有效)
发布评论