[英]Unable to download a blob file with Firefox, but it works in Chrome?
[英]Blob download works in Chrome but not in firefox
我正在使用以下代码片段下载一个 blob 形成的表单 ArrayBuffer
let blob = new window.Blob(dataOrg);
let anchor = document.createElement('a');
anchor.href = URL.createObjectURL(blob);
anchor.download = "downloadssss";
anchor.textContent = 'XXXXXXX';
if (anchor.click) {
anchor.click();
} else {
var evt = document.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
anchor.dispatchEvent(evt);
}
此代码适用于 chrome,但在 Firefox 中不起作用。 我尝试下载的内容是通过 PeerJs 发送的 mp4 文件。
你可以试试下面的代码。
let blob = new window.Blob(dataOrg);
let anchor = document.createElement('a');
anchor.href = URL.createObjectURL(blob);
anchor.download = "downloadssss";
anchor.textContent = 'XXXXXXX';
document.body.appendChild(anchor); // This is required
if (anchor.click) {
anchor.click();
} else {
var evt = document.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
anchor.dispatchEvent(evt);
}
您需要将锚标记附加到文档正文中才能在 Firefox 中工作。 在此更改后,如果您在 index.html 响应中使用内容安全策略标头,则可能会遇到内容安全策略问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.