使用图片 URI 将照片上传到 Firebase 存储
Upload a photo to Firebase Storage with Image URI
我目前正在尝试将照片上传到我的 Apache Cordova 应用中的 Firebase 应用存储中。我目前使用以下代码获取照片的 URI:
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 存储:
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 插件,我尝试这样做时遇到的唯一错误是
Error in Success callbackId: File1733312835 : [object Object]
这只是来自 cordova.js
的一条错误消息
我也知道我的 Firebase 存储设置正确,因为我已经通过模拟器通过添加文件输入并成功将用户添加的任何文件上传到 Firebase 存储对其进行了测试。
是否可以使用这种通过其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?
是的,可以通过 URI 将文件上传到 firebase。但是你必须遵循正确的方法。
1. 文件reading
操作后必须将数据存储在firebase
中completed.you才能使用FileReader.onloadend
为此。
2. 通过使用 data_url
您可以存储到 firebase
.
为了更清楚起见,这里是片段:
function getFileEntry(imgUri) {
window.resolveLocalFileSystemURL(imgUri, function onSuccess(fileEntry) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function() {
filename = "test.jpg";
var storageRef = firebase.storage().ref('/images/' + filename);
var data = 'data:image/jpg;base64,' + imgUri;
storageRef.putString(data, 'data_url').then(function (snapshot) {
console.log('Image is uploaded by base64 format...');
});
};
reader.readAsArrayBuffer(file);
});
},
function onError(err) {
console.log(err);
createNewFileEntry(imgUri);
});
}
我能够使用数据 url 完成上传图片。下面是我的代码:
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!');
});
我目前正在尝试将照片上传到我的 Apache Cordova 应用中的 Firebase 应用存储中。我目前使用以下代码获取照片的 URI:
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 存储:
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 插件,我尝试这样做时遇到的唯一错误是
Error in Success callbackId: File1733312835 : [object Object]
这只是来自 cordova.js
的一条错误消息我也知道我的 Firebase 存储设置正确,因为我已经通过模拟器通过添加文件输入并成功将用户添加的任何文件上传到 Firebase 存储对其进行了测试。
是否可以使用这种通过其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?
是的,可以通过 URI 将文件上传到 firebase。但是你必须遵循正确的方法。
1. 文件reading
操作后必须将数据存储在firebase
中completed.you才能使用FileReader.onloadend
为此。
2. 通过使用 data_url
您可以存储到 firebase
.
为了更清楚起见,这里是片段:
function getFileEntry(imgUri) {
window.resolveLocalFileSystemURL(imgUri, function onSuccess(fileEntry) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function() {
filename = "test.jpg";
var storageRef = firebase.storage().ref('/images/' + filename);
var data = 'data:image/jpg;base64,' + imgUri;
storageRef.putString(data, 'data_url').then(function (snapshot) {
console.log('Image is uploaded by base64 format...');
});
};
reader.readAsArrayBuffer(file);
});
},
function onError(err) {
console.log(err);
createNewFileEntry(imgUri);
});
}
我能够使用数据 url 完成上传图片。下面是我的代码:
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!');
});