From 14812e0cf729c367e733408afbb22b540893eb14 Mon Sep 17 00:00:00 2001 From: David Martins Date: Wed, 4 Oct 2017 16:52:28 -0300 Subject: [PATCH] Added option to send request body modifier function --- src/file-upload/file-uploader.class.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/file-upload/file-uploader.class.ts b/src/file-upload/file-uploader.class.ts index 998352c..1e96861 100644 --- a/src/file-upload/file-uploader.class.ts +++ b/src/file-upload/file-uploader.class.ts @@ -33,6 +33,8 @@ export interface FileUploaderOptions { itemAlias?: string; authTokenHeader?: string; additionalParameter?:{[key: string]: any}; + formatDataFunction?:Function; + formatDataFunctionIsAsync?:boolean; } export class FileUploader { @@ -51,7 +53,9 @@ export class FileUploader { isHTML5: true, filters: [], removeAfterUpload: false, - disableMultipart: false + disableMultipart: false, + formatDataFunction: function (item:FileItem) { return item._file; }, + formatDataFunctionIsAsync: false }; protected _failFilterIndex:number; @@ -318,7 +322,7 @@ export class FileUploader { }); } } else { - sendable = item._file; + sendable = this.options.formatDataFunction(item); } xhr.upload.onprogress = (event:any) => { @@ -360,12 +364,18 @@ export class FileUploader { if (this.authToken) { xhr.setRequestHeader(this.authTokenHeader, this.authToken); } - xhr.send(sendable); xhr.onreadystatechange = function() { if (xhr.readyState == XMLHttpRequest.DONE) { this.response.emit(xhr.responseText) } } + if (this.options.formatDataFunctionIsAsync) { + sendable.then( + (result:any) => xhr.send(JSON.stringify(result)) + ); + } else { + xhr.send(sendable); + } this._render(); }