【问题标题】:Problem with sent image and JSON object / Angular发送图像和 JSON 对象 / Angular 的问题
【发布时间】:2019-02-25 14:32:18
【问题描述】:

我对 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);
  }

这是我的组件:

  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"
}

【问题讨论】:

    标签: angular http-post


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多