火力地堡火力地堡错误存储:预期BLOB或文件:在索引0`put`参数无效

问题描述 投票:1回答:1

我试图从上传本地设备到火力地堡的图像,但是当我打开网页,应该这样做,我得到这个错误,这是行不通的。我得到这个错误之前提交按钮的点击,当我打开网页出现错误。

谢谢

<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);
        } 
javascript firebase firebase-storage
1个回答
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);
    } 
© www.soinside.com 2019 - 2024. All rights reserved.