在移动浏览器上传时,如何获取存储中图像的实时数据库参考

时间:2018-04-23 18:24:57

标签: javascript firebase firebase-storage

我可以在桌面设备和移动浏览器上将图片上传到Firebase Storage。但是移动浏览器不会在实时数据库中保存引用。它在桌面浏览器上。是否有必须为移动浏览器引用的特殊代码?

这里是JS代码(它是一个WIP):

$(fileButton).on('change', function(e) {

  // Get file
  var file = e.target.files[0];

  // Create a storage reference
  var storageRef = firebase.storage().ref(file.name);

  // Upload file to storage bucket
  var uploadTask = storageRef.put(file);

  // Set uploaded file's image URL to real time database
  uploadTask.on('state_changed', function(snapshot) {

    var postKey = firebase.database().ref('posts/').push().key;
    var imageURL = uploadTask.snapshot.downloadURL;
    var updates = {};
    var postData = {
      url: imageURL
    }

    updates['posts/' + postKey] = postData;
    firebase.database().ref().update(updates);

  }, function(err) {
    // if error
    // $('.add-image-error').removeClass('hide');
    console.log('There was a problem uploading the file: ' + err);

  }, function() {
    // if complete, reload the page
    location.reload();
    console.log('File successfully uploaded image and reference');

  });
});

0 个答案:

没有答案