我正在尝试将单个图像上传到 Firebase 存储,然后获取其下载 URL 并将其分配给一个变量。
我可以成功将我的图片上传到 firebase,但是我无法检索下载 url。这是我已经尝试过的方法。
upload() {
let storageRef = firebase.storage().ref();
let success = false;
for (let selectedFile of [(<HTMLInputElement>document.getElementById('file')).files[0]]) {
let router = this.router;
let af = this.af;
let folder = this.folder;
let path = `/${this.folder}/${selectedFile.name}`;
var iRef = storageRef.child(path);
iRef.put(selectedFile).then((snapshot) => {
console.log('Uploaded a blob or file! Now storing the reference at', `/${this.folder}/images/`);
af.list(`/${folder}/images/`).push({ path: path, filename: selectedFile.name })
});
}
// This part does not work
iRef.getDownloadURL().then((url) => {this.image = url});
console.log('IREF IS ' + iRef)
console.log('IMAGEURL IS ' + this.image)
}
控制台日志是这些:
IREF IS gs://my-app-159520.appspot.com/images/Screen Shot 2017-08-14 at 12.19.01.png
view-order.component.ts:134 IMAGEURL IS undefined
Uploaded a blob or file! Now storing the reference at /images/images/
我一直在尝试使用 iRef 引用来获取下载 url,但我一直收到错误。我正在尝试获取 url,以便将其分配给 this.image 变量,然后使用另一个函数将其存储在我的数据库中。
最佳答案
API 已更改。使用以下获取下载URL
snapshot.ref.getDownloadURL().then(function(downloadURL) {
console.log("File available at", downloadURL);
});
关于javascript - 成功将图像上传到 firebase 存储后,Firebase 获取下载 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45714007/