首页 > 解决方案 > Firebase 无法以 Angular 的形式存储图像

问题描述

我想跨数组将图像存储到 Firebase。这是我的html:

 <input type="file" (change)="onFileSelected($event)">

    <button class="btn btn-primary" (click)="onUpload()">Clikc</button>

这是我的图像变量:selectedFile = null; 我在组件中的方法:

 onFileSelected(event) {

this.selectedFile = <File>event.target.files[0];
console.log(this.selectedFile);


}



onUpload() {
    const fb = new FormData();
    fb.append('image', this.selectedFile, this.selectedFile.name)
    this.http.post('https://ng-recipe-.firebaseio.com/', fb).subscribe(
      res => {
        console.log(res)
      }
    )
  }

Database在 Firebase 中发送,但出现错误:

POST https://ng-recipe-book-e.firebaseio.com/ 405 (Method Not Allowed)

有人知道如何通过服务器上的图像传递整个数组。

我想在服务器上发送带有图像的模型:

import { ProductModel } from "./product.model";

export class CategoryModel {

public name: string;
public description: string;
public image: FormData = null;
public products?: ProductModel[];
public file: File;

constructor(name: string, desciption: string, image: FormData = null, products: ProductModel[], file: File) {
    this.name = name;
    this.description = desciption;
    this.image = image;
    this.products = products;
    this.file = file;
}

}

标签: angulartypescriptfirebase

解决方案


推荐阅读