如何解决无法从Firebase的getDownloadURL获取URL
我正在使用Firebase存储将图像存储到其中,图像很容易上传到存储中,但是当我尝试获取图像形式Promise的url时,它不返回url。
const imageSaveHandler = (e) => {
e.preventDefault();
const uploadTask = storage.ref(`images/${image.name}`).put(image);
uploadTask.on(
"state_changed",(error) => {
console.log(error);
},() => {
return storage
.ref(`images/${image.name}`)
.getDownloadURL()
.then((url) => {console.log(url)})
.catch((err) => console.log(err));
}
);
};
解决方法
根据https://firebase.google.com/docs/storage/web/upload-files的示例,看来uploadTask.on
的第三个参数当前具有的处理程序应改为第四个参数:
var uploadTask = storageRef.child('images/rivers.jpg').put(file);
// Register three observers:
// 1. 'state_changed' observer,called any time the state changes
// 2. Error observer,called on failure
// 3. Completion observer,called on successful completion
uploadTask.on('state_changed',function(snapshot){
// Observe state change events such as progress,pause,and resume
// Get task progress,including the number of bytes uploaded and the total number of bytes to be uploaded
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED: // or 'paused'
console.log('Upload is paused');
break;
case firebase.storage.TaskState.RUNNING: // or 'running'
console.log('Upload is running');
break;
}
},function(error) {
// Handle unsuccessful uploads
},function() {
// Handle successful uploads on complete
// For instance,get the download URL: https://firebasestorage.googleapis.com/...
uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
console.log('File available at',downloadURL);
});
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。