最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase Storage Web: How to upload a file - Stack Overflow

programmeradmin1浏览0评论

I added the following function to my website. I am using Firebase Storage but for some reason it seems as if it is not working and I have no idea why.

When you upload a file the progress bar should show the progress of saving/uploading the file to Firebase Storage but that is not the case. Firebase has been initialized and I know it works.

I would greatly appreciate if someone could help me and tell me why the function is not working as described above and how to fix it.

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() {

                    });
    });

}

I added the following function to my website. I am using Firebase Storage but for some reason it seems as if it is not working and I have no idea why.

When you upload a file the progress bar should show the progress of saving/uploading the file to Firebase Storage but that is not the case. Firebase has been initialized and I know it works.

I would greatly appreciate if someone could help me and tell me why the function is not working as described above and how to fix it.

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() {

                    });
    });

}
Share Improve this question edited Dec 26, 2022 at 13:33 Liam 29.7k28 gold badges137 silver badges200 bronze badges asked Dec 23, 2016 at 15:55 SpiderMonkeySpiderMonkey 1612 gold badges2 silver badges11 bronze badges 2
  • Is the upload process working and you want the progress bar working or the whole process? – mcd Commented Dec 25, 2016 at 9:14
  • @maximilian_cs the whole process isn't working – SpiderMonkey Commented Dec 26, 2016 at 19:20
Add a comment  | 

1 Answer 1

Reset to default 36
  1. First change the security Rules

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth == null;
    }
  }
}

  1. Create a file

<!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>

发布评论

评论列表(0)

  1. 暂无评论