How to download a png image from a javascript url?

Asked

Viewed 16 times

-1

I am trying to download an image that comes from a url, this function below works with csv files, . bin already tested and downloaded quietly. But with images, it just redirects to the url and shows the image on the screen. Does anyone have any idea what to do? If someone knows how to do with React if it’s easier help too.

export function downloadUrl(url: string, filename?: string) {
  const element = document.createElement("a");

  element.setAttribute("href", url);

  if (filename) {
    element.setAttribute("download", filename);
  }

  element.style.display = "none";
  document.body.appendChild(element);

  element.click();

  document.body.removeChild(element);
}

<Button onClick={downloadUrl("URL vai aqui")}/>
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.