2

This is my code:

function downloadImage(url) {
      fetch(url, {
     mode: 'no-cors',
    })
    .then(response => response.blob())
     .then(blob => {
        let blobUrl = window.URL.createObjectURL(blob);
        let a = document.createElement('a');
        a.download = url.replace(/^.*[\\\/]/, '');
        a.href = blobUrl;
        document.body.appendChild(a);
        a.click();
        a.remove();
        })
 }

 var url = 'https://c4.wallpaperflare.com/wallpaper/203/636/834/minimalism-landscape-digital- 
 windows-11-hd-wallpaper-preview.jpg';

 downloadImage(url)

When I run this code it's download the image successfully but when I open the image it's shows Sorry, Photos can't open this file because the format is currently unsupported, or the file is corrupted Can anyone tell me please why it's happening and how can I fix this issue.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
anjanna
  • 21
  • 1
  • 4
  • Is this **exactly** the code you are using? If so, you could try to remove the break in the link string and see if it works – Apollo79 Mar 19 '22 at 17:10
  • Remove the spaces in `digital- windows-11` – bron Mar 19 '22 at 18:54
  • @Apollo79 can you give me a hint how can I do that... – anjanna Mar 20 '22 at 04:36
  • use ['https://c4.wallpaperflare.com/wallpaper/203/636/834/minimalism-landscape-digital-windows-11-hd-wallpaper-preview.jpg'](https://c4.wallpaperflare.com/wallpaper/203/636/834/minimalism-landscape-digital-windows-11-hd-wallpaper-preview.jpg) instead of ['https://c4.wallpaperflare.com/wallpaper/203/636/834/minimalism-landscape-digital- windows-11-hd-wallpaper-preview.jpg'](https://c4.wallpaperflare.com/wallpaper/203/636/834/minimalism-landscape-digital- windows-11-hd-wallpaper-preview.jpg). – Apollo79 Mar 20 '22 at 10:04
  • I know, it looks almost similar, but the break inside the string is whitespace (and there are spaces, too), so the URL is not the same. – Apollo79 Mar 20 '22 at 10:06
  • 1
    I have same issue! Any solution yet? Code works fine with .pdf files but not .jpg and .png – DAVID AJAYI Apr 29 '22 at 07:39
  • Any luck? I'm having the same problem with .jpg/.png ... pdf works – OverMars Nov 09 '22 at 06:32

1 Answers1

0

The image url you are using is blocking your request hence you are getting empty blob.

I have tried your code with unsplash image it is working.

   async function downloadImage(url) {
      try {
        const res = await fetch(url);
        const blob = await res.blob();
        const blobUrl = window.URL.createObjectURL(blob);
        const a = document.createElement("a");
        a.download = url.replace(/^.*[\\\/]/, "");
        a.href = blobUrl;
        document.body.appendChild(a);
        a.click();
        a.remove();
      } catch (error) {
        console.log(error);
      }
    }

    const url =
      "https://images.unsplash.com/photo-1644982647844-5ee1bdc5b114?ixlib=rb-1.2.1&ixid=MnwxMjA3fDF8MHxlZGl0b3JpYWwtZmVlZHwxfHx8ZW58MHx8fHw%3D&auto=format&fit=crop&w=500&q=60";

    downloadImage(url);
Harsh Mangalam
  • 1,116
  • 1
  • 10
  • 19