简体   繁体   English

任何解决方法来实现 angular js 以 angular 上传多部分/表单数据的功能? 如何在角度中设置未定义的内容类型?

[英]Any work around to achieve the functionality of angular js uploading multipart/form data in angular ? How to set content type undefined in angular?

This is just a piece of working code in angular js that is used for uploading a file.这只是angular js中用于上传文件的一段工作代码。 Content type the browser detects : 'multipart/form-data;浏览器检测到的内容类型:'multipart/form-data; boundary=----WebKitFormBoundaryXPoOG8abcZmXOpiB' .边界=----WebKitFormBoundaryXPoOG8abcZmXOpiB' 。

$http({
                method: "POST",
                url: url,
                transformRequest: angular.identity,
                headers: {
                    "Authorization": "Bearer " + getToken(),
                    "Content-Type": undefined,
                    "Impersonation": isImpersonation()
                },
                data: formData
            })

Now the same result I want to achieve in angular 11 but not able to set content-type:undefined.现在,我想在 angular 11 中实现相同的结果,但无法设置 content-type:undefined。 also could not find any option of transformRequest: angular.identity,也找不到transformRequest的任何选项:angular.identity,

This is the piece of code I have written down in angular and not able to upload the image.这是我用 angular 写下的一段代码,但无法上传图像。

 return this.httpClient.post<TRsp>(url, formData, {
      headers: {
        "Authorization": "Bearer " + getToken(),
        "Content-Type": 'multipart/form-data',
      }
    }

Can anyone tell me how to convert the above angular js code to angular?谁能告诉我如何将上面的 angular js 代码转换为 angular?

Setting Content-type invalidated your api request sometimes.设置 Content-type 有时会使您的 api 请求无效。 So you need to do is that include empty headers in the request call.所以你需要做的是在请求调用中包含空头。

HTML: HTML:

<input type="file" (change)="fileChange($event)" placeholder="Upload file" accept=".pdf,.doc,.docx">

TS: TS:

fileChange(event) {
    let fileList: FileList = event.target.files;
    if(fileList.length > 0) {
        let file: File = fileList[0];
        let formData:FormData = new FormData();
        formData.append('uploadFile', file, file.name);

        // Do not add content type in headers.
        let headers = new Headers({
          "Authorization": "Bearer " + getToken(),
        });            
        let options = new RequestOptions({ headers: headers });

        this.http.post(`${this.apiEndPoint}`, formData, options)
            .tap(data => console.log("do whatever you want with response data")
            .subscribe();
    }
}

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

相关问题 如何在angular 7中将内容类型设置为multipart/form-data - how to set content type to multipart/form-data in angular 7 Angular 5-如何将null设置为HTTP POST multipart / form-data的内容类型 - Angular 5 - How to set null as content type for HTTP POST multipart/form-data 在 Angular 中使用内容类型 multipart/form-data 发布 - Posting with content type multipart/form-data in Angular Angular 多部分表单的后请求具有错误的内容类型 - Angular post-request with a multipart form has wrong content type Angular 5 &#39;Content-Type&#39;:&#39;multipart/form-data&#39; 被重置为 &#39;application/json&#39; - Angular 5 'Content-Type': 'multipart/form-data' gets reset to 'application/json' 在Angular中如何传递未定义的内容类型标头? - In Angular How to pass content type header as undefined? 如何在 angular 中将数据作为多部分发送 - How to send data as multipart in angular 如何删除 HTTP 拦截器中设置的内容类型以在 angular4 中上传文件 - How to remove Content Type set in HTTP Interceptors for uploading a file in angular4 如何将多部分/表单数据从 Angular 发布到 Nodejs Multer? - How to post multipart/form-data from Angular to Nodejs Multer? 角度5中的表单重置功能 - Form reset functionality in angular 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM