我已将图像上传到Firebase存储,但是当我尝试检索URL时,出现“ uploaded_image.ref不是函数” .....
HTML
<form>
<input id="select_Image" type="file" required>
<button type="submit">Upload Image</button>
</form>
JS
let image_url = "";
function uploadImage {
const image = input_image.files[0];
const path = storage.ref("imagefolder/" + image.name);
const uploaded_image = path.put(image);
const the_url = uploaded_image.ref().getDownloadURL();
image_url = the_url;
alert(image_url);
}
答案 0 :(得分:1)
0
方法和put()
方法都需要调用服务器来完成其工作。因此,他们会返回承诺,而您必须等待该承诺完成才能获得结果。
此外,在上传图像后,您只能获取图像的下载URL。因此,getDownloadURL()
完成后,您才应该致电getDownloadURL()
。
在代码中看起来像这样:
put()
如评论中所述,function uploadImage {
const image = input_image.files[0];
const path = storage.ref("imagefolder/" + image.name);
path.put(image).then(function() {
path.getDownloadURL().then(function(url) {
alert(url);
}
}
}
仅在回调中内部可用。如果您在其他地方使用它,它可能没有您想要的值。因此,任何需要下载URL的代码都应位于回调内部,或从那里调用。
另请参阅:
答案 1 :(得分:0)
据我所知,.put()
函数是异步的,因此您需要处理回调并在该函数中进行工作。您可以使用async,await或仅使用闭包来完成此操作:
const uploaded_image = path.put(image).then((snapshot) => {
alert("Done!");
});
此外,根据the docs,您实际上不需要服务器告诉您URL,因为您应该自己构造它。