我们如何将自定义标头添加到文件上传的http请求(如身份验证)?
答案 0 :(得分:15)
我们使用onBeforeSend
事件:
private onBeforeSend(event) {
event.xhr.setRequestHeader("Authorization", "Bearer " + sessionStorage.getItem("AccessToken"));
}
答案 1 :(得分:2)
不再使用Angular 8 onBeforeSend之后,需要使用标头输入属性
在视图中
<p-fileUpload name="myfile[]" url="./upload.php" [headers]="httpHeaders"></p-fileUpload>
在组件中
httpHeaders:HttpHeaders ;
httpHeaders.set('channel', Channel.AT.toString())
httpHeaders.set('loginId', Settings.user.userId.toString())
httpHeaders.set('sesnId', Settings.auth.token)
httpHeaders.set('commVer', Settings.app.version)
httpHeaders.set('clientIp', Settings.user.clientIp)
httpHeaders.set('uniqueRequestId', Guid.MakeNew().ToString();