【问题标题】:upload file to firebase storage not working将文件上传到firebase存储不起作用
【发布时间】:2017-09-17 16:52:53
【问题描述】:

我确实在 firebase storage 阅读了该文档,但由于某种原因我无法让它工作。

我正在使用 react,我想要的是能够将文件上传到我的 firebase 存储,但我不断收到错误

TypeError: thisRef.put(...).then is not a function

我认为我需要不同的眼光。

这是我的功能

uploadFile = (e) => {
e.preventDefault();
  var file = this.refs.filePath.files[0];
  var storageRef = firebase.storage().ref();

  //dynamically set reference to the file name
  var thisRef = storageRef.child(file.name);

  //put request upload file to firebase storage
  thisRef.put(file).then(function(snapshot) {
  console.log('Uploaded a blob or file!');
  });
}

更新

文件上传到firebase存储,但它一直抱怨承诺(.then)

这是我正在处理的文件GitHub

【问题讨论】:

  • 代码对我来说看起来不错。有什么办法可以在 jsbin 中重现问题,让我们看看?
  • 我用我的 gitHub 存储库更新了它,这可能有帮助吗?文件上传到fb Storage,但它抱怨的是promise

标签: javascript reactjs firebase firebase-storage


【解决方案1】:

您的脚本在我的环境中运行良好。 试试

console.log(thisRef.put(file));

并检查 thisRef.put(file) 是否是 Promise。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Firebase Test</title>
    <script src="https://www.gstatic.com/firebasejs/3.8.0/firebase.js"></script>
    <script>
        // Initialize Firebase
        var config = {
            // your configs
        };
        firebase.initializeApp(config);
    </script>
</head>
<body>
    <input type="file" id="file" name="datafile">
    <script>
        function fileChange(ev) {
            var target = ev.target;
            var file = target.files[0];
            var storageRef = firebase.storage().ref();
            var thisRef = storageRef.child("public/" + file.name);
            thisRef.put(file).then(function (snapshot) {
                console.log('Uploaded a blob or file!');
            });
        }
        var inputFile = document.getElementById('file');
        inputFile.addEventListener('change', fileChange, false);
    </script>
</body>
</html>

【讨论】:

    【解决方案2】:

    你可以试试这样的

    async function _UpdateImage(image) {
    const response = await fetch(image);
    const blob = await response.blob()
    const user = firebase.auth().currentUser;
    if (user) {
        const storageRef = firebase.storage().ref();
        const thisRef = storageRef.child("ProfilePhoto/" + user.uid);
        thisRef.put(blob).then(function (snapshot) {
            console.log('Uploaded a blob or file!',snapshot);
        });
    

    }

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 2019-01-27
      • 2018-03-17
      • 1970-01-01
      • 2021-03-29
      • 2019-09-06
      • 2021-10-06
      • 2019-11-06
      • 2020-04-08
      相关资源
      最近更新 更多