是否可以从图像URI中为我的图像创建文件或Blob对象?
使用我的移动应用程序上的插件Cordova Image Picker,我可以检索如下所示的照片URI:“file:///data/user/0/..../image.jpg”
但是,我现在正在尝试创建一个文件或Blob对象,Google Firebase需要该对象才能上传我的图片。我只是无法弄清楚如何。我尝试的每个解决方案似乎都错了,我认为我从一个完全错误的角度看待它。我是Javascript的新手。非常感谢!
答案 0 :(得分:2)
查看我发布的一段时间的问题,该问题涉及此问题,但对于视频(同样的原则适用):Uploading video to firebase (3.0) storage using cordovaFileTransfer
你需要使用cordova的文件插件读取arrayBuffer,然后使用blob;类似的东西:
var file_path = "root/to/directory";
var name = "filename.jpg";
$cordovaFile.readAsArrayBuffer(file_path, name)
.then(function (success) {
// success
console.log(success);
blob = new Blob([success], {type: "image/jpeg"});
console.log(blob);
var uploadTask = storageRef.child(name).put(blob);
uploadTask.on('state_changed', function(snapshot){
// Observe state change events such as progress, pause, and resume
// See below for more detail
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
}, function(error) {
// Handle unsuccessful uploads
console.log("Error uploading: " + error)
}, function() {
// Handle successful uploads on complete
// For instance, get the download URL: https://firebasestorage.googleapis.com/...
var downloadURL = uploadTask.snapshot.downloadURL;
console.log("Success!", downloadURL);
});
}, function (error) {
// error
console.log("Failed to read file from directory, error.code);
}
如果您的程序向您传递了图像的完整路径,则需要将名称与存储图像的目录的路径分开。只需在最终/
之后查找所有内容