简体   繁体   English

Angular 文件上传不适用于 formData

[英]Angular file upload is not working with formData

I am trying to upload multiple file upload in Angular 7 here is the html code我正在尝试在 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>

Here is the javascript code这是 javascript 代码

exceptionCommentFileChange(event) {
    if (event.target.files.length > 0) {
        this.addExceptionCommentForm.get('documents').setValue(event.target.files);
    }
}

I created formData for sending files through api我创建了用于通过 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();
}

Here is the header request这是 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

And i am trying to change content-type to multipart/form-data it is not changing我正在尝试将内容类型更改为 multipart/form-data 它没有改变

return this.http.post(environment.exceptionsApiUrl + url, params, {headers : {
    'Content-Type': 'multipart/form-data',
    'Accept': 'application/json',
  }}).pipe();

And my request payload is looking something like this我的请求有效负载看起来像这样

------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. 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. Please, let me know if there are any changes to be made.请让我知道是否有任何更改。

Hi I found the solution here.嗨,我在这里找到了解决方案。 When I removed the Content-type it works on my end.当我删除 Content-type 时,它对我有用。 Hope this one will help希望这个会有所帮助

https://github.com/angular/angular/issues/13241 https://github.com/angular/angular/issues/13241

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM