【发布时间】:2019-04-02 13:49:11
【问题描述】:
此代码用于通过 blob 下载 pdf。除了适用于 macOS 和 iOS 的 Safari 12 之外,它在所有浏览器上都能正常工作。甚至 Safari 11 也能正常工作。当我第一次运行代码时,它工作正常,但之后每次它都会给我“WebKitBlobResource error 1”
function downloadFileFromBlob(fileBlob, fileName) {
if (/\bMSIE\b|\bTrident\b/.test($window.navigator.userAgent)) {
$window.navigator.msSaveOrOpenBlob(fileBlob, fileName);
} else {
var fileURL = $window.URL.createObjectURL(fileBlob);
createDownloadElementAndClick(fileURL, fileName);
}
}
function createDownloadElementAndClick(fileURL, fileName) {
var anchorElement = $window.document.createElement('a');
anchorElement.href = fileURL;
anchorElement.target = '_blank';
anchorElement.download = fileName;
var event = $window.document.createEvent("MouseEvents");
event.initEvent("click", true, false);
anchorElement.dispatchEvent(event);
}
【问题讨论】:
-
如果有人仍然面临这个问题,请检查这个。 ???????? stackoverflow.com/a/63040798/11579692
标签: javascript macos pdf safari blob