【发布时间】: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