From b62d570f52e0427dd9acabebc0b1a375cf1c9b7b Mon Sep 17 00:00:00 2001 From: Bohdanko <32868658+77bo@users.noreply.github.com> Date: Fri, 8 Dec 2017 21:53:49 +0200 Subject: [PATCH 1/4] #400 added XHR instance when a request already was done, now it's possible to get response headers. --- src/ngx-uploader/classes/interfaces.ts | 1 + src/ngx-uploader/classes/ngx-uploader.class.ts | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/ngx-uploader/classes/interfaces.ts b/src/ngx-uploader/classes/interfaces.ts index 760b654c..dccb48a4 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; + rawRequest?: XMLHttpRequest; } 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..096ff438 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.rawRequest = xhr; + observer.next({ type: 'done', file: file }); observer.complete(); From 135eacb07dbf7e1ca11af9cebf5390028b182e37 Mon Sep 17 00:00:00 2001 From: Bohdanko <32868658+77bo@users.noreply.github.com> Date: Sat, 9 Dec 2017 23:39:41 +0200 Subject: [PATCH 2/4] #400 exposed only response headers, instead of whole XMLHttpRequest object. --- src/ngx-uploader/classes/interfaces.ts | 2 +- src/ngx-uploader/classes/ngx-uploader.class.ts | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/ngx-uploader/classes/interfaces.ts b/src/ngx-uploader/classes/interfaces.ts index dccb48a4..bdf718ac 100644 --- a/src/ngx-uploader/classes/interfaces.ts +++ b/src/ngx-uploader/classes/interfaces.ts @@ -42,7 +42,7 @@ export interface UploadFile { responseStatus?: number; sub?: Subscription | any; nativeFile?: File; - rawRequest?: XMLHttpRequest; + headers?: { [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 096ff438..1a77df1c 100644 --- a/src/ngx-uploader/classes/ngx-uploader.class.ts +++ b/src/ngx-uploader/classes/ngx-uploader.class.ts @@ -205,7 +205,7 @@ export class NgUploaderService { file.response = xhr.response; } - file.rawRequest = xhr; + file.headers = this.parseResponseHeaders(xhr.getAllResponseHeaders()); observer.next({ type: 'done', file: file }); @@ -297,4 +297,14 @@ export class NgUploaderService { nativeFile: file }; } + + private parseResponseHeaders(httpHeaders: ByteString) { + return httpHeaders.split('\n') + .map(x => x.split(/: */, 2)) + .filter(x => x[0]) + .reduce((ac, x) => { + ac[x[0]] = x[1]; + return ac; + }, {}); + } } From a0490e3adbb232dd61a2dcc5b33d0c4ce199c495 Mon Sep 17 00:00:00 2001 From: Bohdanko <32868658+77bo@users.noreply.github.com> Date: Sun, 10 Dec 2017 16:39:56 +0200 Subject: [PATCH 3/4] #400 added check if headers exists. --- src/ngx-uploader/classes/ngx-uploader.class.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/ngx-uploader/classes/ngx-uploader.class.ts b/src/ngx-uploader/classes/ngx-uploader.class.ts index 1a77df1c..135f785c 100644 --- a/src/ngx-uploader/classes/ngx-uploader.class.ts +++ b/src/ngx-uploader/classes/ngx-uploader.class.ts @@ -299,12 +299,15 @@ export class NgUploaderService { } private parseResponseHeaders(httpHeaders: ByteString) { - return httpHeaders.split('\n') - .map(x => x.split(/: */, 2)) - .filter(x => x[0]) - .reduce((ac, x) => { - ac[x[0]] = x[1]; - return ac; - }, {}); + if (!httpHeaders) { + return null; + } + return httpHeaders.split('\n') + .map(x => x.split(/: */, 2)) + .filter(x => x[0]) + .reduce((ac, x) => { + ac[x[0]] = x[1]; + return ac; + }, {}); } } From 38b59fe7ac37b5d177d393a81f54ced863ae691a Mon Sep 17 00:00:00 2001 From: Bohdanko <32868658+77bo@users.noreply.github.com> Date: Mon, 11 Dec 2017 11:26:36 +0200 Subject: [PATCH 4/4] #400 renamed headers property. Added line to readme. --- README.md | 1 + src/ngx-uploader/classes/interfaces.ts | 2 +- src/ngx-uploader/classes/ngx-uploader.class.ts | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) 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 bdf718ac..5e38ef8c 100644 --- a/src/ngx-uploader/classes/interfaces.ts +++ b/src/ngx-uploader/classes/interfaces.ts @@ -42,7 +42,7 @@ export interface UploadFile { responseStatus?: number; sub?: Subscription | any; nativeFile?: File; - headers?: { [key: string]: string }; + 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 135f785c..01ce6ec1 100644 --- a/src/ngx-uploader/classes/ngx-uploader.class.ts +++ b/src/ngx-uploader/classes/ngx-uploader.class.ts @@ -205,7 +205,7 @@ export class NgUploaderService { file.response = xhr.response; } - file.headers = this.parseResponseHeaders(xhr.getAllResponseHeaders()); + file.responseHeaders = this.parseResponseHeaders(xhr.getAllResponseHeaders()); observer.next({ type: 'done', file: file }); @@ -300,7 +300,7 @@ export class NgUploaderService { private parseResponseHeaders(httpHeaders: ByteString) { if (!httpHeaders) { - return null; + return; } return httpHeaders.split('\n') .map(x => x.split(/: */, 2))