Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

267
Vistas
How can I fix this image download error in javascript?

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)

i can successfully download the image 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

When I use unsplash image url in that time it's work well. Can anyone tell me please why it's happening and how can I fix this issue.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Do like this because you're not defining MIME type while downloading the file.

return fetch(urlEndpoint, options)
  .then((res) => res.blob())
  .then((blob) => URL.createObjectURL(blob))
  .then((href) => {
    Object.assign(document.createElement('a'), {
      href,
      download: 'filename.png',
    }).click();
  });

or else you can do like this

download(filename,url) {
     fetch(url, { 
  mode: 'no-cors',
  })
     .then(response => response.blob())
     .then(blob => URL.createObjectURL(blob))
     .then(uril => {
     var link = document.createElement("a");
     link.href = uril;
     link.download = filename + ".png";
     document.body.appendChild(link);
     link.click();
     document.body.removeChild(link);
     });
 }
about 4 years ago · Juan Pablo Isaza Denunciar

0

The issue here is the Response object , fetch is being called with mode:'no-cors', resulting in the response object to have type as "opaque" and hence using response.blob() will result into a Blob size of 0 and Blob type of "" (read more below in Response.blob()).

The mime-type is set as expected and your code works as expected but since you are making a cross origin request you are getting Response.blob() as empty and the file you subsequently save(download through anchor tag) has no data.

Read more about Response.blob()

Read more about Response.type

Read more about CORS Protocol

to validate the above try and console.log the blob like so :

function downloadImage(url) {
  fetch(url, {
  mode: 'no-cors',
  })
   .then(response => response.blob())
   .then(blob => {
       console.log(blob); //log the blob and check its size;
       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();
       })
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda