![](/img/trans.png)
[英]how to set content type to multipart/form-data in angular 7
[英]Any work around to achieve the functionality of angular js uploading multipart/form data in angular ? How to set content type undefined in angular?
这只是angular js中用于上传文件的一段工作代码。 浏览器检测到的内容类型:'multipart/form-data; 边界=----WebKitFormBoundaryXPoOG8abcZmXOpiB' 。
$http({
method: "POST",
url: url,
transformRequest: angular.identity,
headers: {
"Authorization": "Bearer " + getToken(),
"Content-Type": undefined,
"Impersonation": isImpersonation()
},
data: formData
})
现在,我想在 angular 11 中实现相同的结果,但无法设置 content-type:undefined。 也找不到transformRequest的任何选项:angular.identity,
这是我用 angular 写下的一段代码,但无法上传图像。
return this.httpClient.post<TRsp>(url, formData, {
headers: {
"Authorization": "Bearer " + getToken(),
"Content-Type": 'multipart/form-data',
}
}
谁能告诉我如何将上面的 angular js 代码转换为 angular?
设置 Content-type 有时会使您的 api 请求无效。 所以你需要做的是在请求调用中包含空头。
HTML:
<input type="file" (change)="fileChange($event)" placeholder="Upload file" accept=".pdf,.doc,.docx">
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.