【问题标题】:Read value/content of a file from Firebase Storage从 Firebase 存储中读取文件的值/内容
【发布时间】:2017-06-18 22:23:58
【问题描述】:

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

代替:

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

类似:

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

【问题讨论】:

  • 你心目中的“阅读”和“下载”有什么区别?只需在下载 URL 上执行 XMLHttpRequest 即可。

标签: javascript firebase firebase-storage


【解决方案1】:

目前没有可用的功能,可以直接在 JavaScript 中读取 Firebase 存储中的文件而无需先下载。

如果您认为这真的有用,您可以提交功能请求here

【讨论】:

    【解决方案2】:

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

      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
        })
    

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

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

    【讨论】:

    • 帮了我很多兄弟。非常感谢。
    猜你喜欢
    • 2021-08-30
    • 2017-03-21
    • 2021-05-14
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 2016-07-12
    • 2013-01-23
    相关资源
    最近更新 更多