I am trying to download a file by clicking on a link. But every time, if it is something chrome can open (such as an image) instead of downloading the file, it opens it in the new tab. What should I do to prevent it from opening the file and downlaod it? Here is my code
const downloadElement = document.createElement('a');
downloadElement.href = data;
downloadElement.target = '_blank';
downloadElement.download = data.split('/').pop();
document.body.appendChild(downloadElement);
downloadElement.click();
document.body.removeChild(downloadElement);
No need to apped link tag in body tag. Try below code it's work for me.
In data you need to pass base64 code. other wise it open in new tab !
const downloadElement = document.createElement('a');
downloadElement.download = data.split('/').pop();
downloadElement.href = data;
downloadElement.click();