Reputation: 5438
Note: I want to download the file on the local storage.
Get firebase file
Firebase.storage().ref('file.jpg').getDownloadURL().then((url) => {
var a = document.createElement("a")
document.body.appendChild(a)
a.style = "display: none"
a.href = url
a.download = 'file'
a.click()
}).catch(function(msg) {
// catch error here
})
Tried this method to download with invisible anchor tag. On click the page is redirected to the firebase storage but the downloader is not invoked.
CORS is configured properly
Is there any chance to invoke the browser to download files from firebase storage on button click.
I tried the above code on React JS project but it seems to be broken.
Upvotes: 4
Views: 4788
Reputation: 357
Check if the path is complete and try this code:
storage.ref(path).getDownloadURL().then(function (url) {
var link = document.createElement("a");
if (link.download !== undefined) {
link.setAttribute("href", url);
link.setAttribute("target", "_blank");
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
})
The code on docs doesn't work on my project.
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function(event) {
var blob = xhr.response;
};
xhr.open('GET', url);
xhr.send();
https://firebase.google.com/docs/storage/web/download-files#download_data_via_url
Upvotes: 2