Angular 6文件上传返回“状态:200,但响应:空”,如何获得响应?

时间:2019-07-29 10:32:57

标签: ajax typescript file-upload rxjs angular6

我正在使用ASP.NET Core 2.2 Web API作为后端和Angular6。

当我尝试上传文件时,文件已成功上传,并且收到状态码为200的响应,但我无法从api获取响应正文(我的api返回一个文件名作为响应)。

服务

uploadFile(formData){
  const httpOptionImg = {
    headers: new HttpHeaders()
      .set('Content-Type', 'multipart/form-data') 
      .set('authorization', 'bearer '+localStorage.getItem('access_token'))         
  }
  return ajax.post(this.baseUrl+"/api/Image", formData, httpOptionImg).pipe(
    catchError(this.handleError)
  );
}

组件

async saveCategory() {
    let data = this.categoryForm.value;
    data.HasChild = this.hasChild;

    const formData = new FormData();
    formData.append('file', this.file);

    this.api.uploadFile(formData).subscribe(
      (res) => {
        if (res.status == 200) {
          data.Image = this.res.Message; 
          console.log(data); 
          this.api.saveCategory(data).subscribe(
            (res: Category) => {
              this.ngOnInit();
            });
        }
    });
}  

Ajax和浏览器网络的不同响应

different response of ajax and network of browser

0 个答案:

没有答案