Download image from url in Angular 5

14,048

This is not an angular issue. You can only use download attribute to force download of an image/file, if:

  • the server also says it should be downloaded, or
  • the image comes from the same domain.

It's an issue of cross-origin hrefs.

https://stackoverflow.com/a/17527821/4899523

https://stackoverflow.com/a/49736875/4899523

Share:
14,048
Jorge Monroy
Author by

Jorge Monroy

Updated on June 04, 2022

Comments

  • Jorge Monroy
    Jorge Monroy almost 2 years

    I have an URL in which the image that I need to download is. I want to make a button to download it. I tried this two options:

        const a = document.createElement('a');
            a.href = ImageURL;
            a.download = title;
            document.body.appendChild(a);
            a.click();
    

    and

         window.location.href = t;
    

    But both options just open a new window with the image.

    Is there a way to instead of getting a new window the file just get downloaded?

    • Abdul Rafay
      Abdul Rafay over 5 years
      does the image comes from the same domain?
    • Jorge Monroy
      Jorge Monroy over 5 years
      No, it comes from a url of CARTO, the Static Image API.