简体   繁体   中英

How to download png images by url in javascript or react?

Im trying to download an image, but when i click the download button, the image is just showed, im redirected to the link and it appears. im using this function, where i pass the url as a parameter. This function works to csv extensions, binaries too, but images its not working.. i dont know what to do =(

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(ImageUrl)}/>

You can use FileSaver

FileSaver.saveAs("https://<yourimage>", "<image-name.jpg>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM