diff --git a/README.md b/README.md index 6e0037ec..c1dd2a2d 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ export interface UploadFile { progress: UploadProgress; response?: any; // response when upload is done (parsed JSON or string) responseStatus?: number; // response status code when upload is done + responseHeaders?: { [key: string]: string }; // response headers when upload is done } // output events emitted by ngx-uploader diff --git a/src/ngx-uploader/classes/interfaces.ts b/src/ngx-uploader/classes/interfaces.ts index 760b654c..5e38ef8c 100644 --- a/src/ngx-uploader/classes/interfaces.ts +++ b/src/ngx-uploader/classes/interfaces.ts @@ -42,6 +42,7 @@ export interface UploadFile { responseStatus?: number; sub?: Subscription | any; nativeFile?: File; + responseHeaders?: { [key: string]: string }; } export interface UploadOutput { diff --git a/src/ngx-uploader/classes/ngx-uploader.class.ts b/src/ngx-uploader/classes/ngx-uploader.class.ts index bc602cce..01ce6ec1 100644 --- a/src/ngx-uploader/classes/ngx-uploader.class.ts +++ b/src/ngx-uploader/classes/ngx-uploader.class.ts @@ -205,6 +205,8 @@ export class NgUploaderService { file.response = xhr.response; } + file.responseHeaders = this.parseResponseHeaders(xhr.getAllResponseHeaders()); + observer.next({ type: 'done', file: file }); observer.complete(); @@ -295,4 +297,17 @@ export class NgUploaderService { nativeFile: file }; } + + private parseResponseHeaders(httpHeaders: ByteString) { + if (!httpHeaders) { + return; + } + return httpHeaders.split('\n') + .map(x => x.split(/: */, 2)) + .filter(x => x[0]) + .reduce((ac, x) => { + ac[x[0]] = x[1]; + return ac; + }, {}); + } }