Updating docs and formating files
This commit is contained in:
@@ -25,6 +25,9 @@ Easy to use Angular2 directives for files upload ([demo](http://valor-software.g
|
|||||||
|
|
||||||
- `uploader` - (`FileUploader`) - uploader object. See using in [demo](https://github.com/valor-software/ng2-file-upload/blob/master/demo/components/file-upload/simple-demo.ts)
|
- `uploader` - (`FileUploader`) - uploader object. See using in [demo](https://github.com/valor-software/ng2-file-upload/blob/master/demo/components/file-upload/simple-demo.ts)
|
||||||
|
|
||||||
|
### Events
|
||||||
|
- `onFileSelected` - fires when files are selected and added to the uploader queue
|
||||||
|
|
||||||
## API for `ng2FileDrop`
|
## API for `ng2FileDrop`
|
||||||
|
|
||||||
### Properties
|
### Properties
|
||||||
@@ -39,6 +42,7 @@ Easy to use Angular2 directives for files upload ([demo](http://valor-software.g
|
|||||||
4. `itemAlias` - item alias (form name redefenition)
|
4. `itemAlias` - item alias (form name redefenition)
|
||||||
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.
|
||||||
|
|
||||||
### Events
|
### Events
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,10 @@ import { FileSelectDirective, FileDropDirective, FileUploader } from 'ng2-file-u
|
|||||||
4. `itemAlias` - item alias (form name redefenition)
|
4. `itemAlias` - item alias (form name redefenition)
|
||||||
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.
|
||||||
|
|
||||||
|
### Events
|
||||||
|
- `onFileSelected` - fires when files are selected and added to the uploader queue
|
||||||
|
|
||||||
## FileDrop API
|
## FileDrop API
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ export class FileLikeObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public _createFromObject(object: { size: number, type: string, name: string }): void {
|
public _createFromObject(object: { size: number, type: string, name: string }): void {
|
||||||
// this.lastModifiedDate = copy(object.lastModifiedDate);
|
|
||||||
this.size = object.size;
|
this.size = object.size;
|
||||||
this.type = object.type;
|
this.type = object.type;
|
||||||
this.name = object.name;
|
this.name = object.name;
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ import { Directive, EventEmitter, ElementRef, Input, HostListener, Output } from
|
|||||||
|
|
||||||
import { FileUploader } from './file-uploader.class';
|
import { FileUploader } from './file-uploader.class';
|
||||||
|
|
||||||
// todo: filters
|
|
||||||
|
|
||||||
@Directive({ selector: '[ng2FileSelect]' })
|
@Directive({ selector: '[ng2FileSelect]' })
|
||||||
export class FileSelectDirective {
|
export class FileSelectDirective {
|
||||||
@Input() public uploader: FileUploader;
|
@Input() public uploader: FileUploader;
|
||||||
@@ -27,23 +25,17 @@ export class FileSelectDirective {
|
|||||||
return !!this.element.nativeElement.attributes.multiple;
|
return !!this.element.nativeElement.attributes.multiple;
|
||||||
}
|
}
|
||||||
|
|
||||||
@HostListener('change', ['$event'])
|
@HostListener('change')
|
||||||
public onChange(): any {
|
public onChange(): any {
|
||||||
// let files = this.uploader.isHTML5 ? this.element.nativeElement[0].files : this.element.nativeElement[0];
|
|
||||||
let files = this.element.nativeElement.files;
|
let files = this.element.nativeElement.files;
|
||||||
let options = this.getOptions();
|
let options = this.getOptions();
|
||||||
let filters = this.getFilters();
|
let filters = this.getFilters();
|
||||||
|
|
||||||
// if(!this.uploader.isHTML5) this.destroy();
|
|
||||||
|
|
||||||
this.uploader.addToQueue(files, options, filters);
|
this.uploader.addToQueue(files, options, filters);
|
||||||
this.onFileSelected.emit(files);
|
this.onFileSelected.emit(files);
|
||||||
|
|
||||||
if (this.isEmptyAfterSelection()) {
|
if (this.isEmptyAfterSelection()) {
|
||||||
// todo
|
|
||||||
this.element.nativeElement.value = '';
|
this.element.nativeElement.value = '';
|
||||||
/*this.element.nativeElement
|
|
||||||
.replaceWith(this.element = this.element.nativeElement.clone(true)); // IE fix*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { FileType } from './file-type.class';
|
|||||||
function isFile(value: any): boolean {
|
function isFile(value: any): boolean {
|
||||||
return (File && value instanceof File);
|
return (File && value instanceof File);
|
||||||
}
|
}
|
||||||
// function isFileLikeObject(value:any) {
|
|
||||||
export interface Headers {
|
export interface Headers {
|
||||||
name: string;
|
name: string;
|
||||||
value: string;
|
value: string;
|
||||||
@@ -14,15 +14,18 @@ export interface Headers {
|
|||||||
|
|
||||||
export type ParsedResponseHeaders = { [ headerFieldName: string ]: string };
|
export type ParsedResponseHeaders = { [ headerFieldName: string ]: string };
|
||||||
|
|
||||||
export type FilterFunction = {name:string, fn:(item?:FileLikeObject, options?:FileUploaderOptions)=>boolean};
|
export type FilterFunction = {
|
||||||
|
name: string,
|
||||||
|
fn: (item?: FileLikeObject, options?: FileUploaderOptions) => boolean
|
||||||
|
};
|
||||||
|
|
||||||
export interface FileUploaderOptions {
|
export interface FileUploaderOptions {
|
||||||
allowedMimeType?:Array<string>;
|
allowedMimeType?: string[];
|
||||||
allowedFileType?:Array<string>;
|
allowedFileType?: string[];
|
||||||
autoUpload?: boolean;
|
autoUpload?: boolean;
|
||||||
isHTML5?: boolean;
|
isHTML5?: boolean;
|
||||||
filters?:Array<FilterFunction>;
|
filters?: FilterFunction[];
|
||||||
headers?:Array<Headers>;
|
headers?: Headers[];
|
||||||
method?: string;
|
method?: string;
|
||||||
authToken?: string;
|
authToken?: string;
|
||||||
maxFileSize?: number;
|
maxFileSize?: number;
|
||||||
@@ -33,6 +36,7 @@ export interface FileUploaderOptions {
|
|||||||
itemAlias?: string;
|
itemAlias?: string;
|
||||||
authTokenHeader?: string;
|
authTokenHeader?: string;
|
||||||
additionalParameter?: { [ key: string ]: any };
|
additionalParameter?: { [ key: string ]: any };
|
||||||
|
parametersBeforeFiles?: boolean;
|
||||||
formatDataFunction?: Function;
|
formatDataFunction?: Function;
|
||||||
formatDataFunctionIsAsync?: boolean;
|
formatDataFunctionIsAsync?: boolean;
|
||||||
}
|
}
|
||||||
@@ -41,7 +45,7 @@ export class FileUploader {
|
|||||||
|
|
||||||
public authToken: string;
|
public authToken: string;
|
||||||
public isUploading: boolean = false;
|
public isUploading: boolean = false;
|
||||||
public queue:Array<FileItem> = [];
|
public queue: FileItem[] = [];
|
||||||
public progress: number = 0;
|
public progress: number = 0;
|
||||||
public _nextIndex: number = 0;
|
public _nextIndex: number = 0;
|
||||||
public autoUpload: any;
|
public autoUpload: any;
|
||||||
@@ -54,7 +58,7 @@ export class FileUploader {
|
|||||||
filters: [],
|
filters: [],
|
||||||
removeAfterUpload: false,
|
removeAfterUpload: false,
|
||||||
disableMultipart: false,
|
disableMultipart: false,
|
||||||
formatDataFunction: function (item:FileItem) { return item._file; },
|
formatDataFunction: (item: FileItem) => item._file,
|
||||||
formatDataFunctionIsAsync: false
|
formatDataFunctionIsAsync: false
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -88,7 +92,6 @@ export class FileUploader {
|
|||||||
for (let i = 0; i < this.queue.length; i++) {
|
for (let i = 0; i < this.queue.length; i++) {
|
||||||
this.queue[ i ].url = this.options.url;
|
this.queue[ i ].url = this.options.url;
|
||||||
}
|
}
|
||||||
// this.options.filters.unshift({name: 'folder', fn: this._folderFilter});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public addToQueue(files: File[], options?: FileUploaderOptions, filters?: FilterFunction[] | string): void {
|
public addToQueue(files: File[], options?: FileUploaderOptions, filters?: FilterFunction[] | string): void {
|
||||||
@@ -189,11 +192,11 @@ export class FileUploader {
|
|||||||
return typeof value === 'number' ? value : this.queue.indexOf(value);
|
return typeof value === 'number' ? value : this.queue.indexOf(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public getNotUploadedItems():Array<any> {
|
public getNotUploadedItems(): any[] {
|
||||||
return this.queue.filter((item: FileItem) => !item.isUploaded);
|
return this.queue.filter((item: FileItem) => !item.isUploaded);
|
||||||
}
|
}
|
||||||
|
|
||||||
public getReadyItems():Array<any> {
|
public getReadyItems(): any[] {
|
||||||
return this.queue
|
return this.queue
|
||||||
.filter((item: FileItem) => (item.isReady && !item.isUploading))
|
.filter((item: FileItem) => (item.isReady && !item.isUploading))
|
||||||
.sort((item1: any, item2: any) => item1.index - item2.index);
|
.sort((item1: any, item2: any) => item1.index - item2.index);
|
||||||
@@ -201,11 +204,6 @@ export class FileUploader {
|
|||||||
|
|
||||||
public destroy(): void {
|
public destroy(): void {
|
||||||
return void 0;
|
return void 0;
|
||||||
/*forEach(this._directives, (key) => {
|
|
||||||
forEach(this._directives[key], (object) => {
|
|
||||||
object.destroy();
|
|
||||||
});
|
|
||||||
});*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public onAfterAddingAll(fileItems: any): any {
|
public onAfterAddingAll(fileItems: any): any {
|
||||||
@@ -302,12 +300,7 @@ export class FileUploader {
|
|||||||
let xhr = item._xhr = new XMLHttpRequest();
|
let xhr = item._xhr = new XMLHttpRequest();
|
||||||
let sendable: any;
|
let sendable: any;
|
||||||
this._onBeforeUploadItem(item);
|
this._onBeforeUploadItem(item);
|
||||||
// todo
|
|
||||||
/*item.formData.map(obj => {
|
|
||||||
obj.map((value, key) => {
|
|
||||||
form.append(key, value);
|
|
||||||
});
|
|
||||||
});*/
|
|
||||||
if (typeof item._file.size !== 'number') {
|
if (typeof item._file.size !== 'number') {
|
||||||
throw new TypeError('The file specified is no longer valid');
|
throw new TypeError('The file specified is no longer valid');
|
||||||
}
|
}
|
||||||
@@ -315,6 +308,10 @@ export class FileUploader {
|
|||||||
sendable = new FormData();
|
sendable = new FormData();
|
||||||
this._onBuildItemForm(item, sendable);
|
this._onBuildItemForm(item, sendable);
|
||||||
|
|
||||||
|
const appendFile = () => sendable.append(item.alias, item._file, item.file.name);
|
||||||
|
if (!this.options.parametersBeforeFiles) {
|
||||||
|
appendFile();
|
||||||
|
}
|
||||||
|
|
||||||
// For AWS, Additional Parameters must come BEFORE Files
|
// For AWS, Additional Parameters must come BEFORE Files
|
||||||
if (this.options.additionalParameter !== undefined) {
|
if (this.options.additionalParameter !== undefined) {
|
||||||
@@ -328,7 +325,9 @@ export class FileUploader {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
sendable.append(item.alias, item._file, item.file.name);
|
if (this.options.parametersBeforeFiles) {
|
||||||
|
appendFile();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
sendable = this.options.formatDataFunction(item);
|
sendable = this.options.formatDataFunction(item);
|
||||||
}
|
}
|
||||||
@@ -415,13 +414,8 @@ export class FileUploader {
|
|||||||
|
|
||||||
protected _render(): any {
|
protected _render(): any {
|
||||||
return void 0;
|
return void 0;
|
||||||
// todo: ?
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// protected _folderFilter(item:FileItem):boolean {
|
|
||||||
// return !!(item.size || item.type);
|
|
||||||
// }
|
|
||||||
|
|
||||||
protected _queueLimitFilter(): boolean {
|
protected _queueLimitFilter(): boolean {
|
||||||
return this.options.queueLimit === undefined || this.queue.length < this.options.queueLimit;
|
return this.options.queueLimit === undefined || this.queue.length < this.options.queueLimit;
|
||||||
}
|
}
|
||||||
@@ -438,17 +432,10 @@ export class FileUploader {
|
|||||||
return (status >= 200 && status < 300) || status === 304;
|
return (status >= 200 && status < 300) || status === 304;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* tslint:disable */
|
|
||||||
protected _transformResponse(response: string, headers: ParsedResponseHeaders): string {
|
protected _transformResponse(response: string, headers: ParsedResponseHeaders): string {
|
||||||
// todo: ?
|
|
||||||
/*var headersGetter = this._headersGetter(headers);
|
|
||||||
forEach($http.defaults.transformResponse, (transformFn) => {
|
|
||||||
response = transformFn(response, headersGetter);
|
|
||||||
});*/
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* tslint:enable */
|
|
||||||
protected _parseHeaders(headers: string): ParsedResponseHeaders {
|
protected _parseHeaders(headers: string): ParsedResponseHeaders {
|
||||||
let parsed: any = {};
|
let parsed: any = {};
|
||||||
let key: any;
|
let key: any;
|
||||||
@@ -468,10 +455,6 @@ export class FileUploader {
|
|||||||
return parsed;
|
return parsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*protected _iframeTransport(item:FileItem) {
|
|
||||||
// todo: implement it later
|
|
||||||
}*/
|
|
||||||
|
|
||||||
protected _onWhenAddingFileFailed(item: FileLikeObject, filter: any, options: any): void {
|
protected _onWhenAddingFileFailed(item: FileLikeObject, filter: any, options: any): void {
|
||||||
this.onWhenAddingFileFailed(item, filter, options);
|
this.onWhenAddingFileFailed(item, filter, options);
|
||||||
}
|
}
|
||||||
@@ -503,13 +486,11 @@ export class FileUploader {
|
|||||||
this._render();
|
this._render();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* tslint:disable */
|
|
||||||
protected _onSuccessItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): void {
|
protected _onSuccessItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): void {
|
||||||
item._onSuccess(response, status, headers);
|
item._onSuccess(response, status, headers);
|
||||||
this.onSuccessItem(item, response, status, headers);
|
this.onSuccessItem(item, response, status, headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* tslint:enable */
|
|
||||||
protected _onCancelItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): void {
|
protected _onCancelItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): void {
|
||||||
item._onCancel(response, status, headers);
|
item._onCancel(response, status, headers);
|
||||||
this.onCancelItem(item, response, status, headers);
|
this.onCancelItem(item, response, status, headers);
|
||||||
|
|||||||
Reference in New Issue
Block a user