I have a small simple function that downloads a Base64 file, but the browser just loads and loads, but the download doesn't start.
The download starts because I can see at the bottom that the loading animation is in progress, but it does not appear in the downloads.
Until it finishes, it won't let you download anything else, not a single download starts. But it never finishes, so I have to close the whole browser.
This is not the case for all files.
I tried to use different files and a different browser. But not all files have this problem, so I know the code is correct, just not effective.
What can I do to make the download start smoothly?
function downloadMp3() {
var inputBase64 = document.getElementById('inputBase64').value;
if (!inputBase64) {
return;
}
if (!inputBase64.startsWith('data:audio/')) {
inputBase64 = 'data:audio/mpeg;base64,' + inputBase64;
}
var fileName = 'b64encode_audio.mp3';
var link = document.createElement('a');
link.href = inputBase64;
link.download = fileName;
link.click();
}
document.getElementById('btn').addEventListener('click', downloadMp3)
<textarea id="inputBase64"></textarea>
<button type="button" id="btn">Download</button>