Compare commits
2 Commits
v2.0.0-0
...
koenvander
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
95d57780d0 | ||
|
|
5a6a4471f3 |
@@ -67,7 +67,8 @@ Easy to use Angular2 directives for files upload ([demo](http://valor-software.g
|
|||||||
5. `formatDataFunction` - Function to modify the request body. 'DisableMultipart' must be 'true' for this function to be called.
|
5. `formatDataFunction` - Function to modify the request body. 'DisableMultipart' must be 'true' for this function to be called.
|
||||||
6. `formatDataFunctionIsAsync` - Informs if the function sent in 'formatDataFunction' is asynchronous. Defaults to false.
|
6. `formatDataFunctionIsAsync` - Informs if the function sent in 'formatDataFunction' is asynchronous. Defaults to false.
|
||||||
7. `parametersBeforeFiles` - States if additional parameters should be appended before or after the file. Defaults to false.
|
7. `parametersBeforeFiles` - States if additional parameters should be appended before or after the file. Defaults to false.
|
||||||
|
8. `uploadFilesInSingleRequest` - If 'true', all files in the queue will be uploaded using one multipart request. Defaults to false. Notice this cannot be combined with 'disableMultipart' is 'true'.
|
||||||
|
|
||||||
### Events
|
### Events
|
||||||
|
|
||||||
- `fileOver` - it fires during 'over' and 'out' events for Drop Area; returns `boolean`: `true` if file is over Drop Area, `false` in case of out.
|
- `fileOver` - it fires during 'over' and 'out' events for Drop Area; returns `boolean`: `true` if file is over Drop Area, `false` in case of out.
|
||||||
|
|||||||
@@ -18,8 +18,9 @@ export class SimpleDemoComponent {
|
|||||||
constructor (){
|
constructor (){
|
||||||
this.uploader = new FileUploader({
|
this.uploader = new FileUploader({
|
||||||
url: URL,
|
url: URL,
|
||||||
disableMultipart: true, // 'DisableMultipart' must be 'true' for formatDataFunction to be called.
|
disableMultipart: false, // 'DisableMultipart' must be 'true' for formatDataFunction to be called.
|
||||||
formatDataFunctionIsAsync: true,
|
uploadFilesInSingleRequest: true,
|
||||||
|
formatDataFunctionIsAsync: false,
|
||||||
formatDataFunction: async (item) => {
|
formatDataFunction: async (item) => {
|
||||||
return new Promise( (resolve, reject) => {
|
return new Promise( (resolve, reject) => {
|
||||||
resolve({
|
resolve({
|
||||||
|
|||||||
17113
package-lock.json
generated
Normal file
17113
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -7,7 +7,9 @@
|
|||||||
"lite-server": "lite-server -c demo/bs-config.json",
|
"lite-server": "lite-server -c demo/bs-config.json",
|
||||||
"demo.serve": "run-s build link demo.build lite-server",
|
"demo.serve": "run-s build link demo.build lite-server",
|
||||||
"demo.gh-pages": "run-s build demo.build demo.deploy",
|
"demo.gh-pages": "run-s build demo.build demo.deploy",
|
||||||
"demo.build": "ng build -prod --aot",
|
"demo.build": "ng build",
|
||||||
|
"demo.build.watch": "ng build --watch",
|
||||||
|
"demo.buildOld": "ng build -prod --aot",
|
||||||
"demo.deploy": "gh-pages -d demo/dist",
|
"demo.deploy": "gh-pages -d demo/dist",
|
||||||
"link": "ngm link -p src --here",
|
"link": "ngm link -p src --here",
|
||||||
"lint": "exit 0",
|
"lint": "exit 0",
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ export interface FileUploaderOptions {
|
|||||||
parametersBeforeFiles?: boolean;
|
parametersBeforeFiles?: boolean;
|
||||||
formatDataFunction?: Function;
|
formatDataFunction?: Function;
|
||||||
formatDataFunctionIsAsync?: boolean;
|
formatDataFunctionIsAsync?: boolean;
|
||||||
|
uploadFilesInSingleRequest?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class FileUploader {
|
export class FileUploader {
|
||||||
@@ -59,19 +60,19 @@ export class FileUploader {
|
|||||||
removeAfterUpload: false,
|
removeAfterUpload: false,
|
||||||
disableMultipart: false,
|
disableMultipart: false,
|
||||||
formatDataFunction: (item: FileItem) => item._file,
|
formatDataFunction: (item: FileItem) => item._file,
|
||||||
formatDataFunctionIsAsync: false
|
formatDataFunctionIsAsync: false,
|
||||||
|
uploadFilesInSingleRequest : false
|
||||||
};
|
};
|
||||||
|
|
||||||
protected _failFilterIndex: number;
|
protected _failFilterIndex: number;
|
||||||
|
|
||||||
public constructor(options: FileUploaderOptions) {
|
public constructor(options: FileUploaderOptions) {
|
||||||
this.setOptions(options);
|
this.setOptions(options);
|
||||||
this.response = new EventEmitter<any>();
|
this.response = new EventEmitter<any>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public setOptions(options: FileUploaderOptions): void {
|
public setOptions(options: FileUploaderOptions): void {
|
||||||
this.options = Object.assign(this.options, options);
|
this.options = Object.assign(this.options, options);
|
||||||
|
|
||||||
this.authToken = this.options.authToken;
|
this.authToken = this.options.authToken;
|
||||||
this.authTokenHeader = this.options.authTokenHeader || 'Authorization';
|
this.authTokenHeader = this.options.authTokenHeader || 'Authorization';
|
||||||
this.autoUpload = this.options.autoUpload;
|
this.autoUpload = this.options.autoUpload;
|
||||||
@@ -145,16 +146,24 @@ export class FileUploader {
|
|||||||
this.progress = 0;
|
this.progress = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public uploadItem(value: FileItem): void {
|
public uploadItem(value: FileItem): void {
|
||||||
let index = this.getIndexOfItem(value);
|
this.uploadItems(new Array<FileItem>(value));
|
||||||
let item = this.queue[ index ];
|
}
|
||||||
|
|
||||||
|
public uploadItems(values: FileItem[]): void {
|
||||||
|
values.forEach(element => {
|
||||||
|
let index = this.getIndexOfItem(element);
|
||||||
|
let item = this.queue[ index ];
|
||||||
|
item._prepareToUploading();
|
||||||
|
});
|
||||||
|
|
||||||
let transport = this.options.isHTML5 ? '_xhrTransport' : '_iframeTransport';
|
let transport = this.options.isHTML5 ? '_xhrTransport' : '_iframeTransport';
|
||||||
item._prepareToUploading();
|
|
||||||
if (this.isUploading) {
|
if (this.isUploading) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.isUploading = true;
|
this.isUploading = true;
|
||||||
(this as any)[ transport ](item);
|
(this as any)[ transport ](values);
|
||||||
}
|
}
|
||||||
|
|
||||||
public cancelItem(value: FileItem): void {
|
public cancelItem(value: FileItem): void {
|
||||||
@@ -166,13 +175,20 @@ export class FileUploader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public uploadAll(): void {
|
public uploadAll(): void {
|
||||||
let items = this.getNotUploadedItems().filter((item: FileItem) => !item.isUploading);
|
let items = this.getNotUploadedItems().filter((item: FileItem) => !item.isUploading);
|
||||||
if (!items.length) {
|
if (!items.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
items.map((item: FileItem) => item._prepareToUploading());
|
items.map((item: FileItem) => item._prepareToUploading());
|
||||||
items[ 0 ].upload();
|
|
||||||
|
if (this.options.uploadFilesInSingleRequest){
|
||||||
|
this.uploadItems(items);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
items[ 0 ].upload();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public cancelAll(): void {
|
public cancelAll(): void {
|
||||||
@@ -275,12 +291,27 @@ export class FileUploader {
|
|||||||
public _onCompleteItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): void {
|
public _onCompleteItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): void {
|
||||||
item._onComplete(response, status, headers);
|
item._onComplete(response, status, headers);
|
||||||
this.onCompleteItem(item, response, status, headers);
|
this.onCompleteItem(item, response, status, headers);
|
||||||
let nextItem = this.getReadyItems()[ 0 ];
|
|
||||||
|
if (!this.options.uploadFilesInSingleRequest)
|
||||||
|
{
|
||||||
|
let nextItem = this.getReadyItems()[ 0 ];
|
||||||
|
this.isUploading = false;
|
||||||
|
if (nextItem) {
|
||||||
|
nextItem.upload();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.onCompleteAll();
|
||||||
|
this.progress = this._getTotalProgress();
|
||||||
|
this._render();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public _onCompleteAllItems(items: FileItem[], response: string, status: number, headers: ParsedResponseHeaders): void {
|
||||||
|
items.forEach(item => {
|
||||||
|
item._onComplete(response, status, headers);
|
||||||
|
this.onCompleteItem(item, response, status, headers);
|
||||||
|
});
|
||||||
this.isUploading = false;
|
this.isUploading = false;
|
||||||
if (nextItem) {
|
|
||||||
nextItem.upload();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.onCompleteAll();
|
this.onCompleteAll();
|
||||||
this.progress = this._getTotalProgress();
|
this.progress = this._getTotalProgress();
|
||||||
this._render();
|
this._render();
|
||||||
@@ -295,22 +326,36 @@ export class FileUploader {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
protected _xhrTransport(item: FileItem): any {
|
protected _xhrTransport(items: FileItem[]): any {
|
||||||
let that = this;
|
let that = this;
|
||||||
let xhr = item._xhr = new XMLHttpRequest();
|
let firstItem = items[0];
|
||||||
|
let xhr = firstItem._xhr = new XMLHttpRequest();
|
||||||
let sendable: any;
|
let sendable: any;
|
||||||
this._onBeforeUploadItem(item);
|
|
||||||
|
|
||||||
if (typeof item._file.size !== 'number') {
|
items.forEach(item => {
|
||||||
throw new TypeError('The file specified is no longer valid');
|
this._onBeforeUploadItem(item);
|
||||||
}
|
});
|
||||||
|
|
||||||
|
items.forEach(item => {
|
||||||
|
if (typeof item._file.size !== 'number') {
|
||||||
|
throw new TypeError('The file specified is no longer valid');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if (!this.options.disableMultipart) {
|
if (!this.options.disableMultipart) {
|
||||||
sendable = new FormData();
|
sendable = new FormData();
|
||||||
this._onBuildItemForm(item, sendable);
|
items.forEach(item => {
|
||||||
|
this._onBuildItemForm(item, sendable);
|
||||||
|
});
|
||||||
|
|
||||||
|
const appendFiles = () => {
|
||||||
|
items.forEach(item => {
|
||||||
|
sendable.append(item.alias, item._file, item.file.name)
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const appendFile = () => sendable.append(item.alias, item._file, item.file.name);
|
|
||||||
if (!this.options.parametersBeforeFiles) {
|
if (!this.options.parametersBeforeFiles) {
|
||||||
appendFile();
|
appendFiles();
|
||||||
}
|
}
|
||||||
|
|
||||||
// For AWS, Additional Parameters must come BEFORE Files
|
// For AWS, Additional Parameters must come BEFORE Files
|
||||||
@@ -318,53 +363,55 @@ export class FileUploader {
|
|||||||
Object.keys(this.options.additionalParameter).forEach((key: string) => {
|
Object.keys(this.options.additionalParameter).forEach((key: string) => {
|
||||||
let paramVal = this.options.additionalParameter[ key ];
|
let paramVal = this.options.additionalParameter[ key ];
|
||||||
// Allow an additional parameter to include the filename
|
// Allow an additional parameter to include the filename
|
||||||
if (typeof paramVal === 'string' && paramVal.indexOf('{{file_name}}') >= 0) {
|
if (!this.options.uploadFilesInSingleRequest && typeof paramVal === 'string' && paramVal.indexOf('{{file_name}}') >= 0) {
|
||||||
paramVal = paramVal.replace('{{file_name}}', item.file.name);
|
paramVal = paramVal.replace('{{file_name}}', firstItem.file.name);
|
||||||
}
|
}
|
||||||
sendable.append(key, paramVal);
|
sendable.append(key, paramVal);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.options.parametersBeforeFiles) {
|
if (this.options.parametersBeforeFiles) {
|
||||||
appendFile();
|
appendFiles();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
sendable = this.options.formatDataFunction(item);
|
sendable = this.options.formatDataFunction(firstItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
xhr.upload.onprogress = (event: any) => {
|
|
||||||
let progress = Math.round(event.lengthComputable ? event.loaded * 100 / event.total : 0);
|
|
||||||
this._onProgressItem(item, progress);
|
|
||||||
};
|
|
||||||
xhr.onload = () => {
|
xhr.onload = () => {
|
||||||
let headers = this._parseHeaders(xhr.getAllResponseHeaders());
|
let headers = this._parseHeaders(xhr.getAllResponseHeaders());
|
||||||
let response = this._transformResponse(xhr.response, headers);
|
let response = this._transformResponse(xhr.response, headers);
|
||||||
let gist = this._isSuccessCode(xhr.status) ? 'Success' : 'Error';
|
let gist = this._isSuccessCode(xhr.status) ? 'Success' : 'Error';
|
||||||
let method = '_on' + gist + 'Item';
|
let method = '_on' + gist + 'Item';
|
||||||
(this as any)[ method ](item, response, xhr.status, headers);
|
items.forEach(item => {
|
||||||
this._onCompleteItem(item, response, xhr.status, headers);
|
(this as any)[ method ](item, response, xhr.status, headers);
|
||||||
|
});
|
||||||
|
this._onCompleteAllItems(items, response, xhr.status, headers);
|
||||||
};
|
};
|
||||||
xhr.onerror = () => {
|
xhr.onerror = () => {
|
||||||
let headers = this._parseHeaders(xhr.getAllResponseHeaders());
|
let headers = this._parseHeaders(xhr.getAllResponseHeaders());
|
||||||
let response = this._transformResponse(xhr.response, headers);
|
let response = this._transformResponse(xhr.response, headers);
|
||||||
this._onErrorItem(item, response, xhr.status, headers);
|
items.forEach(item => {
|
||||||
this._onCompleteItem(item, response, xhr.status, headers);
|
this._onErrorItem(firstItem, response, xhr.status, headers);
|
||||||
|
});
|
||||||
|
this._onCompleteAllItems(items, response, xhr.status, headers);
|
||||||
};
|
};
|
||||||
xhr.onabort = () => {
|
xhr.onabort = () => {
|
||||||
let headers = this._parseHeaders(xhr.getAllResponseHeaders());
|
let headers = this._parseHeaders(xhr.getAllResponseHeaders());
|
||||||
let response = this._transformResponse(xhr.response, headers);
|
let response = this._transformResponse(xhr.response, headers);
|
||||||
this._onCancelItem(item, response, xhr.status, headers);
|
items.forEach(item => {
|
||||||
this._onCompleteItem(item, response, xhr.status, headers);
|
this._onCancelItem(firstItem, response, xhr.status, headers);
|
||||||
|
});
|
||||||
|
this._onCompleteAllItems(items, response, xhr.status, headers);
|
||||||
};
|
};
|
||||||
xhr.open(item.method, item.url, true);
|
xhr.open(firstItem.method, firstItem.url, true);
|
||||||
xhr.withCredentials = item.withCredentials;
|
xhr.withCredentials = firstItem.withCredentials;
|
||||||
if (this.options.headers) {
|
if (this.options.headers) {
|
||||||
for (let header of this.options.headers) {
|
for (let header of this.options.headers) {
|
||||||
xhr.setRequestHeader(header.name, header.value);
|
xhr.setRequestHeader(header.name, header.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (item.headers.length) {
|
if (firstItem.headers.length) {
|
||||||
for (let header of item.headers) {
|
for (let header of firstItem.headers) {
|
||||||
xhr.setRequestHeader(header.name, header.value);
|
xhr.setRequestHeader(header.name, header.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user