Added option to send request body modifier function

This commit is contained in:
David Martins
2017-10-04 16:52:28 -03:00
parent 5357a243f6
commit 14812e0cf7

View File

@@ -33,6 +33,8 @@ export interface FileUploaderOptions {
itemAlias?: string; itemAlias?: string;
authTokenHeader?: string; authTokenHeader?: string;
additionalParameter?:{[key: string]: any}; additionalParameter?:{[key: string]: any};
formatDataFunction?:Function;
formatDataFunctionIsAsync?:boolean;
} }
export class FileUploader { export class FileUploader {
@@ -51,7 +53,9 @@ export class FileUploader {
isHTML5: true, isHTML5: true,
filters: [], filters: [],
removeAfterUpload: false, removeAfterUpload: false,
disableMultipart: false disableMultipart: false,
formatDataFunction: function (item:FileItem) { return item._file; },
formatDataFunctionIsAsync: false
}; };
protected _failFilterIndex:number; protected _failFilterIndex:number;
@@ -318,7 +322,7 @@ export class FileUploader {
}); });
} }
} else { } else {
sendable = item._file; sendable = this.options.formatDataFunction(item);
} }
xhr.upload.onprogress = (event:any) => { xhr.upload.onprogress = (event:any) => {
@@ -360,12 +364,18 @@ export class FileUploader {
if (this.authToken) { if (this.authToken) {
xhr.setRequestHeader(this.authTokenHeader, this.authToken); xhr.setRequestHeader(this.authTokenHeader, this.authToken);
} }
xhr.send(sendable);
xhr.onreadystatechange = function() { xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) { if (xhr.readyState == XMLHttpRequest.DONE) {
this.response.emit(xhr.responseText) this.response.emit(xhr.responseText)
} }
} }
if (this.options.formatDataFunctionIsAsync) {
sendable.then(
(result:any) => xhr.send(JSON.stringify(result))
);
} else {
xhr.send(sendable);
}
this._render(); this._render();
} }