Haohao Notes

DREAM OF TECHNICAL ACHIEVEMENT

0%

Safari 手机版下载提示 Mobile Safari download issue : The operation couldn’t be completed. (webkitblobresource error 1.)

上周开发一个功能使用使用Blob

Safari 手机版下载提示 Mobile Safari download issue : The operation couldn’t be completed. (webkitblobresource error 1.)

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
function downloadFile(content, filename) {
var blob = new Blob([content])
var url = window.URL.createObjectURL(blob)
var a = document.createElement('a')
a.style.display = 'none';
a.href = url
a.download = filename
document.body.appendChild(a);
a.click()
window.URL.revokeObjectURL(url);
document.body.removeChild(a);

}

查询Blob支持 Safari 6以上版本
https://developer.mozilla.org/en-US/docs/Web/API/Blob

Blob 浏览器兼容性图

浏览器兼容性图

如果要以编程方式将ancor标记作为解决方案注入DOM,请确保不要过早地清除它。所以延迟一秒清除解决.

解决示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function downloadFile(content, filename) {
var blob = new Blob([content])
var url = window.URL.createObjectURL(blob)
var a = document.createElement('a')
a.style.display = 'none';
a.href = url
a.download = filename
document.body.appendChild(a);
a.click()
setTimeout(() => {
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
}, 1000);
}