【发布时间】:2021-10-28 07:19:54
【问题描述】:
我正在从以下位置上传带有原始 Google 代码的图片: https://firebase.google.com/docs/storage/web/upload-files
uploadImage(image){
var metadata = {
contentType: 'image/jpeg'
};
const file = base64ToFile(image)
// Upload file and metadata to the object 'images/mountains.jpg'
var uploadTask = firebase.storage().ref(this.currentUser.uid).child(imgName + '.jpg').put(file, metadata);
// Listen for state changes, errors, and completion of the upload.
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
(snapshot) => {
// 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;
}
},
(error) => {
// A full list of error codes is available at
// https://firebase.google.com/docs/storage/web/handle-errors
switch (error.code) {
case 'storage/unauthorized':
// User doesn't have permission to access the object
break;
case 'storage/canceled':
// User canceled the upload
break;
// ...
case 'storage/unknown':
// Unknown error occurred, inspect error.serverResponse
break;
}
},
() => {
// Upload completed successfully, now we can get the download URL
uploadTask.snapshot.ref.getDownloadURL().then((downloadURL) => {
console.log('File available at', downloadURL);
return downloadURL
});
}
);
}
上传在我的服务中完成,现在我想在我的文件中等待,直到 donwloadURL 准备好,然后将其保存到我的对象中。我需要将 donwloadURL 直接返回到我的文件中,因为我想在上传并最后保存对象后对我的对象做一些其他的事情。是否有机会等待或返回downloadURL?喜欢
this.dataService.uploadImage(file).then(data => { object.url = data })
【问题讨论】:
标签: javascript firebase firebase-storage