Reputation: 9247
im trying do download file but without success. Im using this code but on click it open image, but i dont want to do that .. i want to download that image. Any suggestion?
toDataURL(url) {
return fetch(url).then((response) => {
return response.blob();
}).then(blob => {
return URL.createObjectURL(blob);
});
}
then
async download() {
const a = document.createElement("a");
a.href = await toDataURL("https://cdn1.iconfinder.com/data/icons/ninja-things-1/1772/ninja-simple-512.png");
a.download = "myImage.png";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
Upvotes: 0
Views: 1525
Reputation: 1481
Not an angular way, but using pure js : Fetch API
function download(url, name) {
fetch(url).then((response) => {
return response.blob().then((b) => {
const a = document.createElement("a");
a.setAttribute("download", name);
a.href = URL.createObjectURL(b);
a.click();
});
});
}
download('https://cdn1.iconfinder.com/data/icons/ninja-things-1/1772/ninja-simple-512.png', 'ninja.png')
Upvotes: 1
Reputation: 26410
What about a simple <a>
download link:
HTML
<a [href]="imageUrl" download>Download image</a>
component.ts
imageUrl:string = "https://cdn1.iconfinder.com/data/icons/ninja-things-1/1772/ninja-simple-512.png"
Upvotes: 1