【问题标题】:Angular 5 - How to set null as content type for HTTP POST multipart/form-dataAngular 5 - 如何将 null 设置为 HTTP POST multipart/form-data 的内容类型
【发布时间】:2018-07-11 04:20:58
【问题描述】:

如何将 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。

【问题讨论】:

  • 您是否在其他地方将内容类型设置为“text/html”?因为据我所知,这不是默认值...
  • 另外,你为什么要将它设置为 null 而不是 multipart/form-data 呢?

标签: angular


【解决方案1】:

您不需要设置任何 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 版一起提供。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多