Skip to content

Instantly share code, notes, and snippets.

@agilworld
Created December 19, 2024 17:54
Show Gist options
  • Save agilworld/e7f7c068305af5434eb4a33ea18ef0e1 to your computer and use it in GitHub Desktop.
Save agilworld/e7f7c068305af5434eb4a33ea18ef0e1 to your computer and use it in GitHub Desktop.
Script download image directly
export function downloadFile(url) {
fetch(url)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.blob();
})
.then((response) => {
const element = document.createElement("a");
const urlRes = URL.createObjectURL(response);
element.href = urlRes;
element.setAttribute("download", `welovephoto.jpg`);
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
URL.revokeObjectURL(urlRes);
})
.catch((error) => {
console.error("Error downloading the image:", error);
});
}
const url = 'https://images.unsplash.com/photo-1615963244664-5b845b2025ee?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3w2ODI1NDd8MHwxfHNlYXJjaHw4fHx0aWdlcnxlbnwwfHx8fDE3MzQ2MDA5Mjh8MA&ixlib=rb-4.0.3&q=80&w=400'
downloadFile(url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment