如何同时将上传的文件保存到Firebase数据库和存储?

时间:2017-03-30 16:40:50

标签: javascript html firebase firebase-realtime-database firebase-storage

我有下面的功能将数据存储到Firebase DB中。我现在要做的是将用户上传的文件存储到Firebase实时数据库和存储中。我决定在底部添加存储引用,但我不断收到错误

TypeError: Cannot read property '0' of null at newApplication (Jobboard.html:209)

Jobboard.html:209

var file = event.target.files[0];

这是JS函数

 function newApplication() {

      var database = firebase.database();
      var applicant_Name = document.getElementById('name').value;
      var applicant_Number = document.getElementById('phone').value; 
      var applicant_email = document.getElementById('email').value; 
      // var resume = document.getElementById('my-file-selector').value;
      var AuthorId = firebase.auth().currentUser.uid;
      var cover_letter = document.getElementById('cover_letter').value;


      var postData = {
              ApplicantName: applicant_Name,
              ApplicantNumber: applicant_Number,
              Applicantemail: applicant_email, 
              // Resume: resume,
              Author: AuthorId,
              Cover_letter: cover_letter,
          };

      var newPostKey = firebase.database().ref().child('Applications').push().key;    

      var updates = {};

          updates['/Applications/' + newPostKey] = postData;

            var files = document.getElementById('files');
            var fileButton = document.getElementById('upload-file-info');
            var file = event.target.files[0];

            var storageRef = firebase.storage().ref('/Resume/' + file.name);
            var uploadtask = storageRef.put(file, metadata);


              // Register three observers:
              // 1. 'state_changed' observer, called any time the state changes
              // 2. Error observer, called on failure
              // 3. Completion observer, called on successful completion
              uploadTask.on('state_changed', function(snapshot){
                // Observe state change events such as progress, pause, and resume
                // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
                var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
                console.log('Upload is ' + progress + '% done');
                switch (snapshot.state) {
                  case firebase.storage.TaskState.PAUSED: // or 'paused'
                    console.log('Upload is paused');
                    break;
                  case firebase.storage.TaskState.RUNNING: // or 'running'
                    console.log('Upload is running');
                    break;
                }
              }, function(error) {
                // Handle unsuccessful uploads
              }, function() {
                // Handle successful uploads on complete
                // For instance, get the download URL: https://firebasestorage.googleapis.com/...
                var downloadURL = uploadTask.snapshot.downloadURL;
              });

      return firebase.database().ref().update(updates); 

  }

如何将上传的文件保存到firebase数据库和存储? 提前谢谢。

0 个答案:

没有答案