URL.createObjectURL
とURL.revokeObjectURL
を組み合わせたdownloadの成功例と失敗例
自動
成功
let a=document.createElement("a");
a.download="a";
a.href=URL.createObjectURL(new Blob(["text"]));
a.click();
URL.revokeObjectURL(a.href)
失敗
let a=document.createElement("a");
a.download="a";
a.click(URL.revokeObjectURL(a.href=URL.createObjectURL(new Blob(["text"]))))
成功
let a=document.createElement("a");
URL.revokeObjectURL(a.href=URL.createObjectURL(new Blob(["text"])),a.click(a.download="a"))
//2回目は失敗
a.click()
手動
失敗
<a id=a download>DL</a><script>
a.onclick=()=>{
a.href=URL.createObjectURL(new Blob(["text"]));
URL.revokeObjectURL(a.href)
}
</script>
失敗
<a id=a download>DL</a><script>
a.onclick=()=>URL.revokeObjectURL(a.href=URL.createObjectURL(new Blob(["text"])))
</script>