firebase - 从 firebase 存储下载 url 为空
问题描述
我正在使用 ionic 4 将图像从相机上传到 firebase 存储。上传顺利,但我很难获得上传 URL。我的代码如下所示:
async getFromCamera(){
this.camera.getPicture({
destinationType: this.camera.DestinationType.DATA_URL,
quality: 25,
correctOrientation: true,
allowEdit:false
}).then(async (imageData) => {
var base64Image = "data:image/jpeg;base64," + imageData;
this.uploadToFireStore(base64Image).then(
(data) => console.log("done to firestore:" + data),
(err) => console.log("The error to upload is:::" + JSON.stringify(err))
)
}, (err) => {
console.log("Error found is:" + err);
});
}
uploadPercent
downloadURL
uploadToFireStore(imageData){
return new Promise<any>((resolve, reject) => {
let storageRef = this.storage.ref('/');
let imageRef = storageRef.child('myimage')
const task = imageRef.putString(imageData, 'data_url')
console.log("Task is:::" + task)
// observe percentage changes
this.uploadPercent = task.percentageChanges();
// get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => {
this.downloadURL = imageRef.getDownloadURL()
console.log("upload percent:" + JSON.stringify(this.uploadPercent))
console.log("download url is::" + JSON.stringify(this.downloadURL))
}
)
)
.subscribe()
})
}
我看到的回应是:
upload percent:{"_isScalar":false,"source":{"_isScalar":false},"operator":{}}
/tab1-tab1-module.js:629 download url is::{"_isScalar":false}
解决方案
Firebase 存储允许您上传文件,因此您无需上传长base64
字符串。为什么还要上传base64
?
我不熟悉this.camera.getPicture
返回什么方法,但很确定它是File
. 在这种情况下:
}).then(imageData => {
this.uploadToFireStore(imageData)
和
uploadToFireStore(imageData){
let storageRef = this.storage.ref('/');
let imageRef = storageRef.child('myimage')
const task = imageRef.upload(imageData, 'data_url')
this.uploadPercent = task.percentageChanges();
task.snapshotChanges().pipe(
finalize( async () => {
await this.downloadURL = imageRef.getDownloadURL().toPromise();
console.log('this.downloadURL', this.downloadURL)
}
)
)
.subscribe()
请注意,它finalize
接受一个async
函数,因为imageRef.getDownloadURL()
返回一个 Observable,所以我们只需要它的一个值,这是我们需要的这个流的唯一值,将它转换为 Promise 看起来更简洁。
推荐阅读
- javascript - 在 Firestore 中运行事务
- awk - 获取匹配模式部分的名称
- lua - 计时器脚本不循环(Roblox Lua)
- excel - 将二维码插入数据库
- reactjs - 将 PDF 文本(任何语言)内容转换为 Html
- docker - 如何使用fabric8 java客户端将docker镜像从私有注册表拉到镜像流下的open shift集群
- android - 我可以在 Android 移动应用程序中存储 mp3 文件吗
- azure - 无法运行 az container exec
- cen-xfs - cen/xfs 如何初始化 ptr_raw_data 以将代码页发送到打印机
- mysql - 组合没有公用表表达式的MYSQL查询