从 Firebase 存储读取文件的值/内容

Phi*_*ipp 5 javascript firebase firebase-storage

是否可以在不使用下载功能的情况下读取文件的值?

代替:

storageRef.child('text.txt').getDownloadURL().then(function() {
  ...
});
Run Code Online (Sandbox Code Playgroud)

就像是:

storageRef.child('text.txt').getValue().then(function(value) {
  alert(value)
});
Run Code Online (Sandbox Code Playgroud)

Tai*_*uan 6

我在 Firebase Storage 中获取文件内容时遇到了同样的问题,但最后,我发现无法直接读取文件内容。不过,我是这样做的。

  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
    })
Run Code Online (Sandbox Code Playgroud)

重要的是配置 CORS 设置。您可以在此处找到说明。

跳过这条指令让我消耗了很多时间:/
我希望其他人避免同样的错误。谢谢。

  • 兄弟对我帮助很大。万分感谢。 (2认同)

AL.*_*AL. 2

目前没有可用的函数可以在 JavaScript 中直接读取 Firebase Storage 中的文件,而无需先下载该文件。

如果您认为这确实有用,您可以在此处提交功能请求。