如何将 ng2-file-upload 与设计令牌身份验证一起使用?

How do I use ng2-file-upload with devise token auth?

我目前正在尝试将 ng2-file-upload 与设计令牌身份验证一起使用。:https://devise-token-auth.gitbook.io/devise-token-auth/conceptual. File uploader currently handles the http request directly and hence, I did not use with angular token rest api service class to upload objects: https://angular-token.gitbook.io/docs/session-management。相反,我必须手动设置 headers 以符合身份验证令牌。

URL = '/api/my_api'
public uploader: FileUploader = new FileUploader({ url: URL});
update_upload_parameters(){
  var uo: FileUploaderOptions = {};
  uo.headers = 
  [
   {name: 'access-token', value : localStorage.getItem("accessToken")}, 
   {name: 'client', value : localStorage.getItem("client")},
   {name: 'expiry', value : localStorage.getItem("expiry")},
   {name: 'token-type', value : localStorage.getItem("tokenType")},
   {name: 'uid', value : localStorage.getItem("uid")}
  ];
  this.uploader.setOptions(uo);

}

HTML 文件

<input id="file2" type="file" ng2FileSelect [uploader]="uploader" />

上传本身成功,但是 post 上传,我看到 session 中的访问令牌不再起作用。我认为这是因为我使用设计令牌身份验证,它根据请求更新令牌,而我没有将其设置到我的本地存储中。如何从响应 header 中手动检索令牌并将其设置到浏览器的本地存储中? (否则在正常请求中由 angular 令牌处理)

想通了,作品:

this.uploader.onCompleteItem = (item:any, response:any, status:any, headers:any)=> {
        localStorage.setItem('accessToken', headers['access-token']);
        localStorage.setItem("expiry", headers['expiry']);
    };
}