How to download image in React.js?

Viewed 911

I use this to download -

<a href={`${link}`} download="file">Download</a>

But it's not working for me. I use the Chrome browser. It just opens the file in the browser.

2 Answers
function download(fileUrl, fileName) {
 let a = document.createElement("a");
 a.href = fileUrl;
 a.setAttribute("download", fileName);
 a.click();
}

Try adding target="_blank":

<a href={`${link}`} download="file" target="_blank" >Download</a>
Related