iOSのSafariでWebKitBlobResource error 1
JavascriptからAPIを叩いて、ファイルをダウンロードしていたところ、iOSのSafariから以下のように言われてしまった。
Safari cannot open the page.
The error was: "The operation couldn't be completed. (WebKitBlobResource error 1)".
MacのSafari、Chromeからは問題なくダウンロードできていた。
環境
- iOS 13.4
問題のコード
async function downloadFile(url) { const res = await axios({ method: 'get', url: url, responseType: 'blob', }) const fileUrl = window.URL.createObjectURL(new Blob([res.data])) const filename = 'sample.zip' let fileLink = document.createElement('a') fileLink.href = fileUrl fileLink.setAttribute('download', filename) document.body.appendChild(fileLink) fileLink.click() URL.revokeObjectURL(fileUrl) document.body.removeChild(fileLink) }
対策
解放処理をsetTimeoutする。
setTimeout(() => { URL.revokeObjectURL(fileUrl) document.body.removeChild(fileLink) }, 200)