3

I've used unsplash API to display photos. when I click on a photo, it shows the photo in a modal and under there is a download button. When I click on that button, it doesn't download the image but instead shows the photo in that tab in the biggest resolution it is available. How do I make that download button actually download the photo?

<a href={photo.links.download} download>Download</a> //this doesn't work to download the photo  
VnoitKumar
  • 1,261
  • 14
  • 27

3 Answers3

1

This simple soultion will work. Add ?force=true at the end of each url href attribute:

<a href={photo.links.download + "?force=true"}>Download</a>
Frostbourn
  • 304
  • 3
  • 20
0

I think that works

const download = e => {
var element = e.target;
var file = new Blob(
  [
    "https://timesofindia.indiatimes.com/thumb/msid-70238371,imgsize-89579,width-400,resizemode-4/70238371.jpg"
  ],
  { type: "image/*" }
);
element.href = URL.createObjectURL(file);
element.download = "image.jpg";
element.click();


};
  return (
    <div className="App">
      <a
        href="https://timesofindia.indiatimes.com/thumb/msid-`70238371,imgsize-89579,width-400,resizemode-4/70238371.jpg"`
        download
        onClick={(e) => download(e)}
      >
        <i className="fa fa-download" />
        download
      </a>
    </div>
  );
0

try this:

downloadFile = (file) => {

  const blob = new Blob(
    [ file ],
    { type: 'image/jpeg' }
  );
  const href = await URL.createObjectURL(blob);
  const link = document.createElement('a');
  link.href = href;
  link.download = 'your file name' + '.jpeg';
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}
jack
  • 313
  • 3
  • 17