Created
March 30, 2017 08:40
-
-
Save fildidi/fb40d487f8a1bca79a2352fbad69bee1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public upload(url: string, body: any, options?: RequestOptionsArgs, forceMethod?: string): Observable<Response> { | |
| return new Observable<Response>((responseObserver: Observer<Response>) => { | |
| const _xhr: XMLHttpRequest = this.browserXhr.build(); | |
| const method = forceMethod || 'POST'; | |
| _xhr.open(method, url); | |
| const onLoad = () => { | |
| const headers: Headers = Headers.fromResponseHeaderString(_xhr.getAllResponseHeaders()); | |
| const responseUrl: string = this.getResponseURL(_xhr) || url; | |
| let responseOptions = new ResponseOptions({ | |
| body: _xhr.response, | |
| status: _xhr.status, | |
| headers: headers, | |
| type: ResponseType.Default, | |
| statusText: _xhr.statusText || 'OK', | |
| url: responseUrl | |
| }); | |
| let response = new Response(responseOptions); | |
| response.ok = (_xhr.status >= 200 && _xhr.status < 300); | |
| if (response.ok) { | |
| responseObserver.next(response); | |
| responseObserver.complete(); | |
| return; | |
| } | |
| responseObserver.error(response); | |
| }; | |
| const onError = (err: ErrorEvent) => { | |
| let responseOptions = new ResponseOptions({ | |
| body: err, | |
| type: ResponseType.Error, | |
| status: _xhr.status, | |
| statusText: _xhr.statusText | |
| }); | |
| responseObserver.error(new Response(responseOptions)); | |
| }; | |
| const onProgress = (progress: ProgressEvent) => { | |
| const percentageComplete = Math.round(progress.loaded / progress.total * 100); | |
| let responseOptions = new ResponseOptions({ | |
| body: percentageComplete, | |
| type: ResponseType.Progress, | |
| status: _xhr.status, | |
| statusText: _xhr.statusText | |
| }); | |
| // For the callee of the .uploadFile method | |
| responseObserver.next(new Response(responseOptions)); | |
| }; | |
| // tuk e problemut!!!!!!!!!!!!!!!! start | |
| let reqOptions = this.addAuthHeader(options, token); | |
| reqOptions.headers.forEach((values, name) => { | |
| _xhr.setRequestHeader(name, values.join(',')); | |
| }); | |
| // tuk e problemut!!!!!!!!!!!!!!!! end | |
| // let formData = new FormData(); | |
| // for(let key in body) { | |
| // if(body.hasOwnProperty(key)) { | |
| // if(key === 'ASSET_FILE_NAME' || key === 'THUMBNAIL_FILE_NAME') { | |
| // return; | |
| // } | |
| // if(key === 'asset') { | |
| // formData.append(key, body[key], body['ASSET_FILE_NAME']) | |
| // } | |
| // if(key ==) '' | |
| // formData.append(key, body[key]); | |
| // } | |
| // } | |
| _xhr.addEventListener('load', onLoad); | |
| _xhr.addEventListener('error', onError); | |
| _xhr.upload.addEventListener('progress', onProgress); | |
| _xhr.send(body); | |
| return () => { | |
| _xhr.removeEventListener('load', onLoad); | |
| _xhr.removeEventListener('error', onError); | |
| _xhr.upload.removeEventListener('progress', onProgress); | |
| _xhr.abort(); | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment