【问题标题】:how can i get the local image url to store in firebase如何获取本地图像 url 以存储在 firebase
【发布时间】:2016-10-07 06:40:17
【问题描述】:
我必须将用户个人资料照片存储在我的 firebase 数据库中。我想存储具有此路径的本地图像:“www/img/avatar.jpeg”。我尝试使用此代码获取图片网址
$scope.user.image="data:img/avatar.jpeg;base64"
但在运行我的代码时,我收到此消息错误:ERR_UNKNOWN_URL_SCHEME。
【问题讨论】:
标签:
angularjs
firebase
firebase-realtime-database
【解决方案1】:
用户必须选择照片,因此您需要在 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 添加到文本框中,您需要将其保存在数据库中。