Angular 10 HttpClient 设置不正确 Content-Type header

Angular 10 HttpClient not setting correct Content-Type header

我只是想用 Angular 10.
将文件发送到后端 在 Postman 中,同样的请求有效! Content-Type header 设置为

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

你必须通过你的 formData-object - 这可能只是一个 typo-issue?

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