Download But it's not working for me. I use the Chrome browser. It just opens the fil" /> Download But it's not working for me. I use the Chrome browser. It just opens the fil" /> Download But it's not working for me. I use the Chrome browser. It just opens the fil"/>

How to download image in React.js?

2.3k Views Asked by At

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

There are 2 best solutions below

3
Aliaga Aliyev On
function download(fileUrl, fileName) {
 let a = document.createElement("a");
 a.href = fileUrl;
 a.setAttribute("download", fileName);
 a.click();
}
1
Sebastian On

Try adding target="_blank":

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