I tried so many method to download image from firebase but download URL is nor working as what i expected, Its open the image in new tab.
The firebase function:
export async function addMedia(location: "initial" | "prep" | "merge" | "sribe" | "other", file: string) {
let fileName = uuidv4();
const storageRef = ref(storage, `/${location}/${fileName}`);
let downloadLink = "";
await uploadString(storageRef, file, "data_url").then(async (snapshot) => {
await getDownloadURL(snapshot.ref).then((link: any) => {
downloadLink = link;
})
})
return downloadLink;
I’m using file-saver dependencies for my download purpose its working fine.
File download function:
const downloadImage = (url: any, name: any) => {
console.log(url, name, "image details");
FileSaver.saveAs(url,name);
}
The fire base URL im getting from firebase function:
When i press download i get below result :
2
Answers
I use base64Url to download image its working fine for me.im saving base64Url in firebase as a string
Stop combining
await
andthen
; use one of the other, but not both.So:
Remember that you need to use
await
orthen
when calling thisaddMedia
too, which means that you can’t call it in your code that renders the UI. If you want to show the image in a component in the UI, store the download URL in the state of the component and use it from there, as shown in: