如何将图像和数据添加到Firebase

时间:2019-03-27 12:51:37

标签: javascript html5 firebase firebase-realtime-database

我想在数据库中发送/添加通知数据,该数据库包含带有一些文本数据的图像,因此我无法在Firebase中添加此数据。我尝试了一些仅用于数据插入的代码,但是它不起作用,以及如何在Firebase中添加图像?

我正在添加手动创建的数据库以进行通知。我想使用图片添加更多通知

数据库

enter image description here

这是我的html表单

<form method=post enctype="multipart/form-data">
                                    <div class="row">
                                        <div class="col-md-5">
                                            <div class="form-group">
                                                <label>Title</label>
                                                <input type="text" id="title" class="form-control" placeholder="Company" >
                                            </div>
                                        </div>
                                        <div class="col-md-3">
                                            <div class="form-group">
                                                <label>Image</label>
                                                <input type="image" class="form-control" placeholder="Image">
                                            </div>
                                        </div> 
                                        <div class="col-md-4">
                                            <div class="form-group">
                                                <label for="exampleInputEmail1">Redeem Steps</label>
                                                <input type="text" id="redeem" class="form-control" placeholder="Redeem Steps">
                                            </div>
                                        </div>
                                    </div>


                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="form-group">
                                                <label>Description</label>
                                                <textarea rows="5" id="description" class="form-control" placeholder="Here can be your description"></textarea>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="row">
                                    <div class="col-md-12" >
                                        Image 1 <span style="color:red">*</span><!--<input type="file" id="image" name="img1"  required>-->    
                                    </div>
                                </div>


                                    <button type="submit" id="submitBtn" class="btn btn-info btn-fill pull-right" onclick="submitclick()">Send notification</button>
                                    <div class="clearfix"></div>
                                </form>

这是js文件

var title=document.getElementById("title");
var redeem=document.getElementById("redeem");
var description=document.getElementById("description");
var image=document.getElementById("image");
var submitBtn=document.getElementById("submitBtn");
var Id=1;
function submitclick(){

    var firebaseref=firebase.database().ref();

    var messagetitle=title.value;
    var messageredeem=redeem.value;
    var messagedescription=description.value;
    //var messageimage=image.value;

    console.log(messagetitle);
    console.log(messageredeem);
    console.log(messagedescription);
    //console.log(messageimage);


    //firebaseref.child("notification").set("vinit");
    //firebaseref.child("notification").set("2");
    //firebaseref.child("notification").set("messagedescription");
    //firebaseref.child("notification").set("messageimage");
    firebase.database().ref('notification/'+Id).set({
        title : messagetitle,
        redeem : messageredeem,
        description : messagedescription
        image : messageimage
      });

      console.log(Id);
Id++;
console.log(Id);
}

1 个答案:

答案 0 :(得分:3)

您不能将图像直接上传到Firebase数据库。您必须先将映像上传到Firebase存储中,然后根据需要将映像名称/ location / downloadUrl存储在数据库中。最好不要存储下载URL。

const file = ...
const metadata = { contentType: 'image/jpeg' }; // or whatever you want
const storageRef = firebase.storage().ref();
const uploadTask = storageRef.child(`images/${file.name}`).put(file, metadata);


uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, snapshot => {
  // If you want to show upload progress, do whatever you want with progress...
  const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
  switch (snapshot.state) {
    case firebase.storage.TaskState.PAUSED:
      console.log('Upload is paused');
      break;
    case firebase.storage.TaskState.RUNNING:
      console.log('Upload is running');
      break;
  }
}, error => {
  console.log(error);
}, () => {
  // upload finished with success, you can get the download URL
  uploadTask.snapshot.ref.getDownloadURL().then(downloadURL => {
    console.log(downloadURL);
  });
});

如果要将downloadUrl存储到数据库中,则必须将downloadUrl存储到变量中,或将数据库集放入上载完成的回调中。 数据库集部分应以这种方式工作:

// The id should foloow your database structure,
// based on your posted image, should look like this:
// `noti_${id}` where the id is a number.
firebase.database().ref(`notification/${id}`).set({
  ...uploadData,
  image: downloadUrl
});

此外,我几乎不建议您使用异步等待来处理承诺并使用云Firestore代替实时数据库。