【发布时间】:2021-06-30 02:33:33
【问题描述】:
我在这个模块中的主要目标是在 Firebase 存储中上传用户的个人资料图片,并将照片 URL 存储在 Firestore(Cloud Firestore)中。
注意:
- 我上传的图片只有 JPG 和 PNG 格式。
- 这是具有 Ionic 和 Angular 的跨平台应用程序
我尝试以下场景:
- 我直接将图片推送到Firestore而不上传到Firebase Cloud Storage所以图片的名称被转换为长字符的“data:image,base64” 在其中,我不知道这个场景在我的数据库或系统中是否可以,因为它运行完美,并且图像是可检索的。我希望你能在这种情况下帮助我。
更新错误:
这是用户的字段:
update-profile.component.ts
userId: string;
fname: string;
studentId: string;
@ViewChild('filePicker') filePickerRef: ElementRef<HTMLInputElement>;
@Input() showPreview = true;
//The Selected Image in the File Explorer
selectedImage: any;
constructor(private auth: AuthService,
private afs: AngularFirestore,
private loadingCtrl: LoadingController,
private toaster: ToastController,
private storage: AngularFireStorage,
private popOverCtrl: PopoverController) { }
ngOnInit() {
this.profileEditSub = this.auth.user$.subscribe(user => {
this.userId = user.userId;
this.fname = user.userName;
this.studentId = user.userSchoolId;
this.selectedImage = user.userPhoto;
});
}
onSubmit(form: NgForm){
const user = form.value.fname;
const studentId = form.value.studentId;
this.onUpdateUser(user, studentId);
}//
async onUpdateUser(name: string, studentNo: string){
const loading = await this.loadingCtrl.create({
message: 'Updating...',
spinner: 'crescent',
showBackdrop: true
});
loading.present();
const imageUrl = await this.uploadFile(this.userId, this.selectedImage);
this.afs.collection('user').doc(this.userId).update({
'userName': name,
'userSchoolId': studentNo,
'userPhoto': imageUrl, // Photo URL from Firebase Storage will be saved in here.
'editedAt': Date.now()
})
.then(() => {
loading.dismiss();
this.toast('Update Success', 'success');
this.closePopOver();
})
.catch(error => {
loading.dismiss();
this.toast(error.message, 'danger');
})
}//
async uploadFile(id, files):Promise<any> {
if (files && files.length) {
try {
const task = await this.storage.ref('images').child(id).put(files[0]);
return this.storage.ref(`images/${id}`).getDownloadURL().toPromise();
} catch (error) {
console.log(error);
}
}
}
onPickImage() {
this.filePickerRef.nativeElement.click();
}
onFileChosen(event: Event) {
const pickedFile = (event.target as HTMLInputElement).files[0];
if (!pickedFile) {
return;
}
const fr = new FileReader();
fr.onload = () => {
const dataUrl = fr.result.toString();
this.selectedImage = dataUrl;
};
fr.readAsDataURL(pickedFile);
}
update-profile.component.html
<!-- For the Image -->
<div class="picker">
<img
class="img"
[src]="selectedImage"
*ngIf="selectedImage && showPreview"
>
</div>
<input type="file" accept=".jpg,.png" *ngIf="usePicker" #filePicker (change)="onFileChosen($event)"/>
<ion-button class="image-btn" (click)="onPickImage()">
<ion-icon name="camera" slot="icon-only"></ion-icon>
</ion-button>
<!-- Other Textual Information -->
<form #f="ngForm" (ngSubmit)="onSubmit(f)">
<ion-list lines="full">
<ion-item>
<ion-label position="floating">Full Name:</ion-label>
<ion-input name="fname" required type="text" [(ngModel)]="fname" #userCtrl="ngModel"></ion-input>
</ion-item>
<ion-item>
<ion-label position="floating">Student No:</ion-label>
<ion-input name="studentId" required type="number" [(ngModel)]="studentId" #studentIds="ngModel"></ion-input>
</ion-item>
<ion-button class="ion-margin" type="submit" expand="block" shape="round" [disabled]="!f.valid">Edit User</ion-button>
</ion-list>
</form>
【问题讨论】:
-
我相信你不需要使用 FileReader。你可以直接上传。检查这个:stackoverflow.com/questions/46988902/…
-
谢谢你。但我想问题是图像没有生成下载网址,因为我检查了变量“uploadedImage”及其未定义。
-
但是你确认上传成功了吗?你总是需要一个错误回调来捕捉
storage.upload中的错误 -
很遗憾,它没有上传成功。但是我有一个问题,直接在 Firestore 中上传图片好不好,这是我的替代解决方案之一。
-
我认为直接上传没有问题。我相信它应该像我添加的链接中提到的那样工作
标签: angular firebase ionic-framework google-cloud-firestore