Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I download image in React?

I want to try to download an image by clicking on a button, but when I click on the button, it is not downloading the image, but it is directly opening the image. But I want to download the image, so how can I download the image in React?

<a
  href="https://timesofindia.indiatimes.com/thumb/msid-70238371,imgsize-89579,width-400,resizemode-4/70238371.jpg"
  download
>
  <i className="fa fa-download" />
</a>
like image 401
Dharmesh Avatar asked Dec 05 '25 07:12

Dharmesh


1 Answers

I came across this SO question trying to figure out how to download a PNG image and found that Shubham Verma's answer didn't quite do it for me as the downloaded file couldn't be opened. I needed to use arraybuffer to convert the image.

Here's the code that worked.

function App() {
  const download = e => {
    console.log(e.target.href);
    fetch(e.target.href, {
      method: "GET",
      headers: {}
    })
      .then(response => {
        response.arrayBuffer().then(function(buffer) {
          const url = window.URL.createObjectURL(new Blob([buffer]));
          const link = document.createElement("a");
          link.href = url;
          link.setAttribute("download", "image.png"); //or any other extension
          document.body.appendChild(link);
          link.click();
        });
      })
      .catch(err => {
        console.log(err);
      });
  };
  return (
    <div className="App">
      <a
        href="https://upload.wikimedia.org/wikipedia/en/6/6b/Hello_Web_Series_%28Wordmark%29_Logo.png"
        download
        onClick={e => download(e)}
      >
        <i className="fa fa-download" />
        download
      </a>
    </div>
  );
}

Codesandbox: https://codesandbox.io/s/dreamy-gould-h1l72

P.S.: The download approach was taken from the following answer, but I used plain fetch instead of Axios. Download binary file with Axios

like image 199
alanmynah Avatar answered Dec 07 '25 23:12

alanmynah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!