lawchihon
lawchihon

Reputation: 13

Rename or Download File from Firebase Storage in JavaScript

Is there any way to rename or download the file from the firebase storage?

I don't see the rename method nor download method.

I tried to download the file by the url and it doesn't work at all

var blob = null;
var xhr = new XMLHttpRequest(); 
xhr.open("GET", "downloadURL"); 
xhr.responseType = "blob";
xhr.onload = function() 
{
    blob = xhr.response;//xhr.response is now a blob object
    console.log(blob);
}
xhr.send();

It returns

No 'Access-Control-Allow-Origin' header is present on the requested resource.

Upvotes: 0

Views: 2396

Answers (2)

Mike McDonald
Mike McDonald

Reputation: 15953

Two things here:

1) you want to use the getDownloadURL() method (docs) to get a public download URL, that way you can simply drop your item in an <img> tag, allow users to click on it to download it, or use an XMLHttpRequest to get the bytes.

2) you'll want to enable CORS on your objects, see: Firebase Storage and Access-Control-Allow-Origin

Upvotes: 2

Endless
Endless

Reputation: 37806

Trigger click with javascript...

<a href="downloadURL" download="filename.txt">download filename.txt</a> 

Upvotes: 0

Related Questions