使用 Primeng 的 FileUpload 组件在上传前添加自定义 headers

Add custom headers before upload with Primeng's FileUpload component

如何将自定义 headers 添加到文件上传的 http 请求(如身份验证)?

我们使用 onBeforeSend 事件:

private onBeforeSend(event) {
    event.xhr.setRequestHeader("Authorization", "Bearer " + sessionStorage.getItem("AccessToken"));
}

在Angular 8后不再使用onBeforeSend需要使用headers input 属性

在视图中

<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();