【问题标题】:Firebase error Firebase Storage: Invalid argument in `put` at index 0: Expected Blob or FileFirebase 错误 Firebase 存储:索引 0 处的“put”中的参数无效:预期的 Blob 或文件
【发布时间】:2019-02-11 16:17:40
【问题描述】:

我正在尝试将图像从本地设备上传到 Firebase,但是当我打开应该执行此操作的网页时,我收到此错误并且它不起作用。 我在单击提交按钮之前收到此错误,因为我打开页面时收到错误。

谢谢

<div id="filesubmit">
  <input type="file" id="caricaFile" class="file-select" accept="image/*"/>
  <button class="file-submit">SUBMIT</button>
</div>
          try{// Initialize Firebase
          var config = {
            apiKey: "AIzaSyA5-N-IEZDs9XYhmMCpSvyByp0OlTR-bhs",
            authDomain: "hackathon-76f01.firebaseapp.com",
            databaseURL: "https://hackathon-76f01.firebaseio.com",
            projectId: "hackathon-76f01",
            storageBucket: "hackathon-76f01.appspot.com",
            messagingSenderId: "1040124036693"
          };

          firebase.initializeApp(config);

          var storageRef = firebase.storage().ref(); // use the Blob or File API

        // Create a reference to 'mountains.jpg'
        var mountainsRef = storageRef.child('mountains.jpg');

        // Create a reference to 'images/mountains.jpg'
        var mountainImagesRef = storageRef.child('images/mountains.jpg');

        var file = document.getElementById("caricaFile").value

        storageRef.put(file).then(function(snapshot) {
          console.log('Uploaded a blob or file!');
        });

        var metadata = {
          contentType: 'image/jpeg',
        };

        // Upload the file and metadata
        var uploadTask = storageRef.child('images/mountains.jpg').put(file, metadata);
         }
        catch(err){
        console.log("errore");
        console.log(err);
        } 

【问题讨论】:

  • caricaFile 到底是什么?是否有与此代码一起使用的 HTML 表单?
  • @DougStevenson 我编辑并添加了相关的html代码
  • 在代码运行之前,您是否使用该表单选择了要上传的文件?

标签: javascript firebase firebase-storage


【解决方案1】:

我不知道是否需要 try/catch 块,但我想它应该像这样工作:

<div id="filesubmit">
   <input type="file" id="caricaFile" class="file-select" accept="image/*"/>
   <button class="file-submit" onclick="fileUpload(event)">SUBMIT</button>
</div>

      try{// Initialize Firebase
      var config = {
        apiKey: "AIzaSyA5-N-IEZDs9XYhmMCpSvyByp0OlTR-bhs",
        authDomain: "hackathon-76f01.firebaseapp.com",
        databaseURL: "https://hackathon-76f01.firebaseio.com",
        projectId: "hackathon-76f01",
        storageBucket: "hackathon-76f01.appspot.com",
        messagingSenderId: "1040124036693"
      };

      firebase.initializeApp(config);
        function fileUpload(event){
            var storageRef = firebase.storage().ref(); // use the Blob or File API
            var file=event.target.files[0]
            storageRef.put(file).then(function(snapshot) {
            console.log('Uploaded a blob or file!');
            });

            var metadata = {
            contentType: 'image/jpeg',
            };

            // Upload the file and metadata
            var uploadTask = storageRef.child('images/mountains.jpg').put(file, metadata);
        }

     }
    catch(err){
    console.log("errore");
    console.log(err);
    } 

【讨论】:

  • 请你解释一下怎么做?我知道如何将功能附加到按钮,但不知道如何添加文件
  • 然后在脚本中: function myfunction(){ //你所做的所有事情 }例如:w3schools.com/jsref/event_onclick.asp
  • 现在我在点击提交按钮后遇到同样的错误
  • 我刚刚检查过我之前是如何做到的:html 中的更改:onclick="myfunction()" javascript 中的更改:var file myfunction(event){ file = event.target.files[0] }现在“文件”将成为文件类型。
  • 我解决了使用这个突击队获取文件:“document.getElementById("caricaFile").files[0]”。感谢您的帮助!
猜你喜欢
  • 2021-10-02
  • 2020-08-11
  • 2018-01-07
  • 2020-11-22
  • 2021-03-09
  • 2020-04-23
  • 1970-01-01
  • 2016-12-27
  • 2018-03-17
相关资源
最近更新 更多