【问题标题】:Angular 10 HttpClient not setting correct Content-Type headerAngular 10 HttpClient 未设置正确的 Content-Type 标头
【发布时间】:2020-10-11 22:46:52
【问题描述】:

我只是尝试使用 Angular 10 将文件发送到后端。
在 Postman 中,同样的请求也有效! Content-Type 标头设置为

multipart/form-data; boundary=--------------------------131632757107984585618022

但是当我对 Angular 执行相同操作时,它只会将 Content-Type 设置为 text/plain;charset=UTF-8,然后我从后端收到 unknown content type: "text/plain;charset=UTF-8" 错误。

我找到的唯一解决方案是将Content-Type 设置为undefined 但是由于Cannot read property 'length' of undefined at http.js:105 错误,这不起作用。

我的代码:

upload(event: any) {
    const files = event.target.files;
    const formData: FormData = new FormData();
    for (const file of files) {
        formData.append('files', file, file.name);
    }
    return this.http.post(environment.API_URL + '/add', FormData)
    .toPromise()
    .then((res) => console.log(res))
    .catch((err) => console.log(err));
}

【问题讨论】:

    标签: angular httpclient multipartform-data content-type


    【解决方案1】:

    您必须传递您的 formData-object - 这可能只是一个错字问题?

    return this.http.post(environment.API_URL + '/add', formData);

    【讨论】:

    • 嗯,这很尴尬。非常感谢!
    猜你喜欢
    • 2013-12-10
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    相关资源
    最近更新 更多