3

I have a problem when I tried to download a blob as a file in Chrome.
the code goes like this

const link = document.createElement('a');
const href= window.URL.createObjectURL(blobData)
link.href = href;
link.download = `${fileName}.${fileExtension}`;
link.onclick = () => {
    window.URL.revokeObjectURL(href);
};
link.click();

The problem is Chrome will pop up a save as window, and by the time user confirm the download path the revokeObjectURL already called which caused the download failed.

I know setTimeout might solve the problem, but it is confusing. I wonder if there is a solid solution to know when to call revokeObjectURL.

Boyang Li
  • 53
  • 1
  • 5
  • What about your `blobData`, how did you create it? – Nimer Awad Dec 18 '19 at 05:53
  • it is a Blob Object created outside my working scope. It should be legit, and it works when Chrome doesn't ask for download path. – Boyang Li Dec 18 '19 at 05:58
  • @Kaiido It's a different question. I think the information you linked to was relevant, however, and it's annoying that the auto-comment seems to no longer appear. :-/ Link is now gone. Can you add it as a comment here? – Brad Dec 18 '19 at 06:06
  • @Brad I'm sorry but I disagree. Even though the question there had far more information (noise?) they were actually asking the exact same thing, since they were actually facing the exact same problem: calling revokeObjectURL too soon was breaking the download. https://stackoverflow.com/questions/50686147/releasing-memory-from-blob-creation-object-url-in-writing-file-to-clients-dis I can't reuse my Mjolnir... – Kaiido Dec 18 '19 at 06:09

0 Answers0