[英]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.