[英]Angular file upload is not working with formData
我正在尝试在 Angular 7 中上传多个文件上传,这里是 html 代码
<form [formGroup]="exceptionChangeStatusForm" (ngSubmit)="submitChangeStatusForm()" enctype="multipart/form-data">
<div class="form-group">
<label>Documents:</label>
<input type="file" name="documents" multiple (change)="exceptionCommentFileChange($event)"/>
</div>
<div class="form-group" style="text-align:center;">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
这是 javascript 代码
exceptionCommentFileChange(event) {
if (event.target.files.length > 0) {
this.addExceptionCommentForm.get('documents').setValue(event.target.files);
}
}
我创建了用于通过 api 发送文件的 formData
submitChangeStatusForm() {
const formData = new FormData();
const documents = this.addExceptionCommentForm.get('documents').value;
for ( let i = 0; i < documents.length; i++) {
formData.append('documents', documents[i], documents[i]['name']);
}
this.http.post(environment.exceptionsApiUrl, formData).pipe();
}
这是 header 请求
Accept: application/json
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
Authorization: Bearer eyJ0eXAiOiJKV
Connection: keep-alive
Content-Length: 179473
Content-Type: application/json; charset=utf-8
Host: localhost
Origin: http://localhost:4200
Referer: http://localhost:4200/v2/exceptions/validation
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36
我正在尝试将内容类型更改为 multipart/form-data 它没有改变
return this.http.post(environment.exceptionsApiUrl + url, params, {headers : {
'Content-Type': 'multipart/form-data',
'Accept': 'application/json',
}}).pipe();
我的请求有效负载看起来像这样
------WebKitFormBoundaryqe260o3UOMqEKW2A
Content-Disposition: form-data; name="documents"; filename="1.jpeg"
Content-Type: image/jpeg
When i try from postman it works, but from angular it is not working, I am getting 400 response from api when i try from angular. 请让我知道是否有任何更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.