Firebase Storage Web:如何上传文件

时间:2016-12-23 15:55:08

标签: web firebase firebase-storage

我在网站上添加了以下功能。我正在使用Firebase存储,但由于某种原因,它似乎无法正常工作,我不知道为什么。

当您上传文件时,进度条应显示将文件保存/上传到Firebase存储的进度,但情况并非如此。 Firebase已初始化,我知道它有效。

如果有人能帮助我并告诉我为什么功能不能如上所述以及如何解决它,我将不胜感激。

function AddMed() {
        var uploader = document.getElementById("uploader");
    var fileButton = document.getElementById("fileButton");
    var email = sessionStorage.getItem("email");
  //Listen for file selection

    fileButton.addEventListener('change', function (e) {
    // Get File 
        var file = e.target.files[0];
    //Create a storage ref

        var storageRef = firebase.storage().ref('file/' + file.name);
    /** folder name will be email, 
    Will have to transfer variable from page to page and files will be .jpeg**/
    //Upload file 

        var task = storageRef.put(file);
    //Update progress bar

        task.on('state_changed',
                function progress(snapshot) {
                var percentage = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
                uploader.value = percentage;
            },
            function complete() {

                    });
    });

}

1 个答案:

答案 0 :(得分:11)

  1. 首先更改安全规则
  2. service firebase.storage {
      match /b/{bucket}/o {
        match /{allPaths=**} {
          allow read, write: if request.auth == null;
        }
      }
    }

    1. 创建文件
    2. <!DOCTYPE html>
      <html>
      <head>
      	<title>Firebase Web Basics</title>
      
      	<link href="https://fonts.googleapis.com/css?family=Raleway:300,400,500,700" rel="stylesheet">
      
      	<script src="https://use.fontawesome.com/939e9dd52c.js"></script>
      
      	<link rel="stylesheet" type="text/css" href="style.css">
      </head>
      <body>
      
      	<div class="mainDiv" align="right">
      		<h1 align="left">Firebase File Upload</h1>
      		<progress id="uploader" value="0" max="100">0%</progress>
      		<input type="file" id="fileButton" value="upload"/>
      	</div>
      
      
      
      <script src="https://www.gstatic.com/firebasejs/3.7.4/firebase.js"></script>
      <script>
        // Initialize Firebase
        var config = {
          apiKey: "******************************",
          authDomain: "******************************",
          databaseURL: "******************************",
          storageBucket: "******************************",
          messagingSenderId: "******************************"
        };
        firebase.initializeApp(config);
        //-------------------------------------
        
        var uploader = document.getElementById('uploader');
        var fileButton =         document.getElementById('fileButton');
        fileButton.addEventListener('change', function(e){
        var file = e.target.files[0];
        var storageRef = firebase.storage().ref('img/'+file.name);
        var task = storageRef.put(file);
        task.on('state_changed', function progress(snapshot) {
          var percentage = (snapshot.bytesTransferred/snapshot.totalBytes)*100;
          uploader.value = percentage;
      
        }, function error(err) {
      
      
        },function complete() {
      
        });
      });  
        
        
      </script>
      
      <script src="fileup.js">
      </script>
      </body>
      </html>