Read value/content of a file from Firebase Storage

本秂侑毒 提交于 2020-12-04 03:44:50

问题


Is it possible to read the value a file without using the download function?

Something instead of:

storageRef.child('text.txt').getDownloadURL().then(function() {
  ...
});

Something like:

storageRef.child('text.txt').getValue().then(function(value) {
  alert(value)
});

回答1:


There is currently no available function to directly read a file in Firebase Storage in JavaScript without downloading it first.

You can file a Feature Request here, if you think this would be really useful.




回答2:


I had the same trouble with getting content of the file in Firebase Storage, but finally, I found there is no way to read the file content directly. However, I did it like this.

  fileRef.getDownloadURL()
    .then(url => {
      var xhr = new XMLHttpRequest();
      xhr.responseType = 'json'; 
      xhr.onload = function(event) {
        var json= xhr.response;
        console.log(json);      // now you read the file content
      };
      xhr.open('GET', url);
      xhr.send();
    })
    .catch(err => {
        // process exceptions
    })

What is important was configuring the CORS settings. You can find the instructions here.

Skipping this instruction made me consume much time :/
I hope others avoid the same mistakes. Thanks.



来源:https://stackoverflow.com/questions/41992202/read-value-content-of-a-file-from-firebase-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!