繁体   English   中英

Angular 5-如何将null设置为HTTP POST multipart / form-data的内容类型

[英]Angular 5 - How to set null as content type for HTTP POST multipart/form-data

如何将null设置为HTTP POST multipart / form-data的内容类型

下面是无法正常工作的代码:

let abc = new FormData();
abc.append('name', "dgdhghdhd");
abc.append('file', this.doc); //doc is a file object
abc.append('product_image', "ssss");

console.log("form data  ---->")
console.log(abc)
this.http.post('http://localhost:8080/create_product', abc, {
  headers: new HttpHeaders().set('Content-Type', '')
})
  .subscribe(data => {
    console.log(data)

  });

这是行不通的,我可以看到内容类型默认设置为text / html。

您无需设置任何Content-Type即可使用。 如果内容类型被更改,您可能还会执行其他操作。 这是我使用的代码:

export class AppComponent  {
  name = 'Angular 6';
  content = null;
  constructor(private http: HttpClient) {
    const formData: FormData = new FormData();
    formData.append('test', 'test');
    http.post('https://httpbin.org/post', formData).subscribe((next) => this.content = next);
  }
}

我在stackblitz上发布了一个工作示例,该示例发布到https://httpbin.org上 ,该镜像反映了该请求,您可以看到它已正确发布。 它使用Angular 6,但我认为这与版本5并没有改变。新的HttpClient随版本4一起提供。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM