发送图像和JSON对象/角度的问题

时间:2019-02-25 14:32:18

标签: angular http-post

我对Angular Post方法有疑问。我想将图像和模型发送到服务器。 这是我的服务:

  pushFileToStorage(file: File, ct:CategoryModel): Observable<HttpEvent<{}>> {
    const formdata: FormData = new FormData();

    formdata.append('file', file);
    const req = new HttpRequest('POST', 'api/cateogry/saveCategory',  {formdata ,ct} ,{
      reportProgress: true,
      responseType: 'text'
    });

    return this.http.request(req);
  }

这是我的commponenet:

  this.uploadService.pushFileToStorage(this.currentFileUpload, this.category).subscribe(event => {
      if (event.type === HttpEventType.UploadProgress) {
        this.progress.percentage = Math.round(100 * event.loaded / event.total);
      } else if (event instanceof HttpResponse) {
        console.log('File is completely uploaded!');
      }
    });

我有错误:

{
    "timestamp": "2019-02-25T13:44:22.093+0000",
    "status": 415,
    "error": "Unsupported Media Type",
    "message": "Content type 'multipart/form-data;boundary=--------------------------125126563049882524399172;charset=UTF-8' not supported",
    "path": "/api/product/createCategory"
}

1 个答案:

答案 0 :(得分:0)

使用 formdata.append('file',JSON.stringify(file));

请访问: POST JSON fails with 415 Unsupported media type, Spring 3 mvc