需要帮助获取 Firebase 扩展调整图片大小后的缩放图 URL。之前我能通过以下代码实现,但在启动该函数后,图片未显示。将 Firestore 上的 imgURL 与 Firebase 存储上的下载 URL 进行比较后,似乎函数在路径的末尾添加了图像尺寸(500x500)(image_123 -> image_123_500x500)。
- 我尝试在 Firebase 扩展设置中查找如何排除它,但没有成功。(可能错过了) - 我想下一个选项就是通过获取调整图像后的 URL 来调整代码。 但我要么得到未定义或错误。而且我无法想出实现“先上传图像”然后“提交表单”这两个功能而不创建两个单独的函数的方法。
- 我尝试在 Firebase 扩展设置中查找如何排除它,但没有成功。(可能错过了) - 我想下一个选项就是通过获取调整图像后的 URL 来调整代码。 但我要么得到未定义或错误。而且我无法想出实现“先上传图像”然后“提交表单”这两个功能而不创建两个单独的函数的方法。
upload($event: any) {
this.file = $event.target.files[0];
this.image = true;
}
async submitHandler() {
if (this.image === true) {
// Generate random ID
const randomId = Math.random().toString(36).substring(2);
// The storage path
const path = `products/${this.productForm.value.model}_${randomId}`;
// Reference to storage bucket
const ref = this.afStorage.ref(path);
// The main task (upload Image to Firestorage)
this.task = this.afStorage.upload(path, this.file);
// Get the URL from ref (Firestorage), added to the form and submit to FIrestore
this.task
.snapshotChanges()
.pipe(
finalize(async () => {
this.downloadURL = await ref.getDownloadURL().toPromise();
/*this.afs
.collection('products')
.add({ downloadURL: this.downloadURL, path });*/
this.loading = true;
this.productForm.value.imgUrl = this.downloadURL;
this.productForm.value.user = this.auth.getUserEmail();
const formValue = this.productForm.value;
try {
await this.afs.collection('products').add(formValue);
console.log(formValue);
this.success = true;
} catch (err) {
console.error(err);
}
})
) // To display URL
.subscribe();
this.loading = false;
} else {
alert('Please upload the picture of product');
}
}