问题描述
我正在尝试将单个图片上传到Firebase存储,然后获取其下载网址并将其分配给变量。
I am trying to upload a single image to Firebase Storage, then grab its download url and assign this to a variable.
我可以将我的图片上传到firebase成功,但我无法检索下载网址。这是我已经尝试过的。
I can upload my image to firebase successfully, however I cannot retrieve the download url. here is what I have tried already.
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引用来抓取下载网址,但我一直在收到错误。我试图抓住网址,以便将其分配给this.image变量,然后使用其他函数将其存储在我的数据库中。
I have been trying to use the iRef reference to grab the download url but I keep getting errors. I am trying to grab the url so I can assign it to the this.image variable and then store it in my database using another function.
推荐答案
API已更改。使用以下方法获取downloadURL
The API has changed. Use the following to get downloadURL
snapshot.ref.getDownloadURL().then(function(downloadURL) {
console.log("File available at", downloadURL);
});
这篇关于Firebase成功上传到firebase存储后获取下载URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!