使用图片URI将照片上传到Firebase存储 [英] Upload a photo to Firebase Storage with Image URI
问题描述
我目前正在尝试将照片上传到我的Apache Cordova应用中的Firebase应用存储中。我目前使用以下代码获取照片的URI:
I am currently attempting to upload a photo to my Firebase app's storage in my Apache Cordova app. I currently get the photo's URI with the following code:
function getPhotoFromAlbum() {
navigator.camera.getPicture(onPhotoURISuccess, onFail, {
quality: 50,
sourceType: navigator.camera.PictureSourceType.SAVEDPHOTOALBUM,
destinationType: navigator.camera.DestinationType.FILE_URI
});
}
function onPhotoURISuccess(imageURI) {
var image = document.getElementById('image');
image.style.display = 'block';
image.src = imageURI;
getFileEntry(imageURI);
}
然后我尝试将图像转换为文件使用以下函数将其推送到我的Firebase存储:
And then am attempting to convert the image into a file and push it to my Firebase storage with the following function:
function getFileEntry(imgUri) {
window.resolveLocalFileSystemURL(imgUri, function success(fileEntry) {
console.log("got file: " + fileEntry.fullPath);
var filename = "test.jpg";
var storageRef = firebase.storage().ref('/images/' + filename);
var uploadTask = storageRef.put(fileEntry);
}, function () {
// If don't get the FileEntry (which may happen when testing
// on some emulators), copy to a new FileEntry.
createNewFileEntry(imgUri);
});
}
我同时安装了文件和相机cordova插件,唯一的错误我当我尝试这样做时得到
I have both the file and the camera cordova plugins installed, the only errors I get when I attempt to do this is
Error in Success callbackId: File1733312835 : [object Object]
这只是来自cordova.js的错误消息
Which is just an error message from cordova.js
我也知道我已正确设置Firebase存储空间,因为我已通过模拟器测试它,方法是添加文件输入并成功上传用户添加的任何文件到Firebase存储空间。
I also know I have my Firebase storage set up correctly because I have tested it through an emulator by adding a file input and successfully uploading whatever file the user added, to the Firebase storage.
是否可以使用这种通过URI将图像转换为文件然后上传的方法将文件上传到Firebase存储?如果是这样,这样做的正确方法是什么/我做的方式有什么问题?
Is it possible to upload a file to Firebase storage using this method of converting an image to a file through its URI, and then uploading it? If so, what is the correct way to do so / what is wrong with the way i'm doing it?
推荐答案
我能够通过使用数据URL完成上传图像。以下是我的代码:
I was able to accomplish uploading an image by using a data url. Below is my code:
var filename = "test.jpg";
var storageRef = firebase.storage().ref('/images/' + filename);
var message = 'data:image/jpg;base64,' + imageUri;
storageRef.putString(message, 'data_url').then(function (snapshot) {
console.log('Uploaded a data_url string!');
});
这篇关于使用图片URI将照片上传到Firebase存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!