Hi I'm trying to download an Firebase Storage Image to the pc via Button click. I tried this but to no sucess. The Image is just opening in my browser. How can I force the pc to download the Image or is this even posible because of cross-origin ?
What I tried:
HTML:
<button pButton pRipple type="button" (click)="downloadFile(userAttendance)" icon="pi pi-download" class="p-button-rounded"></button>
TS:
downloadFile(userAttendance: UserAttendance) {
let link = document.createElement("a");
link.setAttribute('type', 'hidden');
link.download = "krankmeldung_von_" + userAttendance.user.name + ' ' + userAttendance.user.surname;
link.href = userAttendance.attendance.ill_recipe_fp!;
document.body.appendChild(link);
link.click();
link.remove();
}
Also:
<a href="{{ userAttendance.attendance.ill_recipe_fp }}" download="Image.jpg">Download</a>
UPADTE
I used this as a guide and updated the functions
HTML:
<button pButton pRipple type="button" (click)="downloadResource(userAttendance)" icon="pi pi-download" class="p-button-rounded"></button>
TS:
downloadFile(blobUrl: string, filename: string) {
let link = document.createElement("a");
link.download = filename;
link.href = blobUrl;
document.body.appendChild(link);
link.click();
link.remove();
}
downloadResource(userAttendance: UserAttendance) {
let filename: string = "krankmeldung_von_" + userAttendance.user.name + ' ' + userAttendance.user.surname;
let url: string = userAttendance.attendance.ill_recipe_fp!;
if (!filename) filename = url!.split('\\')!.pop()!.split('/')!.pop()!;
fetch(url, {
headers: new Headers({
'Origin': location.origin
}),
mode: 'no-cors'
})
.then(response => response.blob())
.then(blob => {
let blobUrl = window.URL.createObjectURL(blob);
this.downloadFile(blobUrl, filename);
})
.catch(e => console.error(e));
}
and now it WORKS! but its downloading an .txt file and not an .jpg or .png.