Blob createObjectURL 下载在 Firefox 中不起作用(但在调试时有效)

编程入门 行业动态 更新时间:2024-10-28 03:17:46
本文介绍了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 中不起作用(但在调试时有效)

本文发布于:2023-11-13 21:34:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585378.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:但在   中不   Blob   createObjectURL   Firefox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!