Skip to content

Commit

Permalink
Merge pull request #402 from 77bo/response-headers-support
Browse files Browse the repository at this point in the history
Support for #400, handling response headers after upload request
  • Loading branch information
retailify authored Dec 11, 2017
2 parents 05cf604 + 38b59fe commit 42ed768
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/ngx-uploader/classes/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface UploadFile {
responseStatus?: number;
sub?: Subscription | any;
nativeFile?: File;
responseHeaders?: { [key: string]: string };
}

export interface UploadOutput {
Expand Down
15 changes: 15 additions & 0 deletions src/ngx-uploader/classes/ngx-uploader.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}, {});
}
}

0 comments on commit 42ed768

Please sign in to comment.