如何将本地图像URL存储在firebase中

时间:2016-06-07 11:23:39

标签: angularjs firebase firebase-realtime-database

我必须在我的firebase数据库中存储用户个人资料照片。我想存储具有此路径的本地图像:" www / img / avatar.jpeg"。我尝试使用此代码获取图片网址

$scope.user.image="data:img/avatar.jpeg;base64"

但是在运行我的代码时,我收到此消息错误:ERR_UNKNOWN_URL_SCHEME

1 个答案:

答案 0 :(得分:0)

用户必须选择照片,因此您需要HTML中的文件选择器:

<input type="file" id="file" name="file" />

然后你可以用JavaScript处理它:

function handleFileSelect(e) {
  var file = e.target.files[0];
  // Get a reference to the location where we'll store our photos
  var storageRef = storage.ref().child('chat_photos');

  // Get a reference to store file at photos/<FILENAME>.jpg
  var photoRef = storageRef.child(file.name);
  // Upload file to Firebase Storage
  var uploadTask = photoRef.put(file);
  uploadTask.on('state_changed', null, null, function() {
    // When the image has successfully uploaded, we get its download URL
    var downloadUrl = uploadTask.snapshot.downloadURL;
    // Set the download URL to the message box, so that the user can send it to the database
    textInput.value = downloadUrl;
  });
}
file.addEventListener('change', handleFileSelect, false);

此代码来自Zero To App talk at Google I/O。完整代码可在this gist中找到。

将下载URL添加到文本框的位置,您需要将其保存在数据库中。