Compare commits
1 Commits
duykhang53
...
mattmcspar
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f577becb29 |
11
.travis.yml
11
.travis.yml
@@ -1,13 +1,11 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "10.15.0"
|
||||
|
||||
sudo: required
|
||||
services:
|
||||
- xvfb
|
||||
- "6"
|
||||
|
||||
before_install:
|
||||
- google-chrome-stable --headless --disable-gpu --remote-debugging-port=9222 http://localhost &
|
||||
- export CHROME_BIN=chromium-browser
|
||||
- export DISPLAY=:99.0
|
||||
- sh -e /etc/init.d/xvfb start
|
||||
|
||||
script:
|
||||
- npm run pretest
|
||||
@@ -17,7 +15,6 @@ after_success:
|
||||
- ./node_modules/.bin/codecov
|
||||
|
||||
addons:
|
||||
chrome: stable
|
||||
firefox: "latest"
|
||||
apt:
|
||||
sources:
|
||||
|
||||
57
README.md
57
README.md
@@ -43,6 +43,63 @@ Easy to use Angular2 directives for files upload ([demo](http://valor-software.g
|
||||
|
||||
```public uploader:FileUploader = new FileUploader({url: URL}); ```
|
||||
|
||||
## Using ***ng2-file-upload*** with multiple uploads and other parameters
|
||||
To add extra information (i.e. title, description) when uploading multiple files:
|
||||
|
||||
In your template:
|
||||
```
|
||||
<tr *ngFor="let item of uploader.queue">
|
||||
<td><strong>{{ item?.file?.name }}</strong></td>
|
||||
|
||||
**<td><input type="text" [(ngModel)]="item.formData['title']" placeholder="Title of File"></td>**
|
||||
**<td><input type="text" [(ngModel)]="item.formData['description']" placeholder="File Description"></td>**
|
||||
<td nowrap>{{ item?.file?.size/1024/1024 | number:'.2' }} MB</td>
|
||||
<td >
|
||||
<div class="progress" style="margin-bottom: 0;">
|
||||
<div class="progress-bar" role="progressbar" [ngStyle]="{ 'width': item.progress + '%' }"></div>
|
||||
</div>
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<span *ngIf="item.isSuccess"><i class="glyphicon glyphicon-ok"></i></span>
|
||||
<span *ngIf="item.isCancel"><i class="glyphicon glyphicon-ban-circle"></i></span>
|
||||
<span *ngIf="item.isError"><i class="glyphicon glyphicon-remove"></i></span>
|
||||
</td>
|
||||
<td nowrap>
|
||||
<button type="button" class="btn btn-success btn-xs"
|
||||
(click)="item.upload()" [disabled]="item.isReady || item.isUploading || item.isSuccess">
|
||||
<span class="glyphicon glyphicon-upload"></span> Upload
|
||||
</button>
|
||||
<button type="button" class="btn btn-warning btn-xs"
|
||||
(click)="item.cancel()" [disabled]="!item.isUploading">
|
||||
<span class="glyphicon glyphicon-ban-circle"></span> Cancel
|
||||
</button>
|
||||
<button type="button" class="btn btn-danger btn-xs"
|
||||
(click)="item.remove()">
|
||||
<span class="glyphicon glyphicon-trash"></span> Remove
|
||||
</button>
|
||||
</td>
|
||||
|
||||
</tr>
|
||||
```
|
||||
You can add inputs outside of a form, and use ```[(ngModel)]``` to add keys to the ```item``` object.
|
||||
|
||||
```
|
||||
**<td><input type="text" [(ngModel)]="item.formData['title']" placeholder="Title of File"></td>**
|
||||
**<td><input type="text" [(ngModel)]="item.formData['description']" placeholder="File Description"></td>**
|
||||
```
|
||||
Inside your upload component, add the following:
|
||||
|
||||
```
|
||||
this.uploader.onBuildItemForm = (item, form) => {
|
||||
form.append('title', item.formData.title);
|
||||
form.append('description', item.formData.description);
|
||||
console.log(item);
|
||||
};
|
||||
|
||||
```
|
||||
If you are using `Node.js` with `multer`, the extra information will not show up in the `req.files` object. You will have to pull the data from the `req.body` object.
|
||||
|
||||
|
||||
## API for `ng2FileSelect`
|
||||
|
||||
### Properties
|
||||
|
||||
@@ -48,7 +48,6 @@ module.exports = function (config) {
|
||||
|
||||
if (process.env.TRAVIS) {
|
||||
configuration.browsers = ['Chrome_travis_ci'];
|
||||
configuration.singleRun = true;
|
||||
}
|
||||
|
||||
if (process.env.SAUCE) {
|
||||
|
||||
16759
package-lock.json
generated
16759
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -84,8 +84,8 @@
|
||||
"jasmine-core": "2.5.2",
|
||||
"jasmine-data-provider": "2.2.0",
|
||||
"jasmine-spec-reporter": "3.2.0",
|
||||
"karma": "^4.2.0",
|
||||
"karma-chrome-launcher": "^3.1.0",
|
||||
"karma": "1.4.0",
|
||||
"karma-chrome-launcher": "^2.0.0",
|
||||
"karma-cli": "^1.0.1",
|
||||
"karma-coverage-istanbul-reporter": "^1.3.0",
|
||||
"karma-jasmine": "^1.0.2",
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
import { Directive, ElementRef, EventEmitter, HostListener, Input, Output } from '@angular/core';
|
||||
import { Directive, EventEmitter, ElementRef, HostListener, Input, Output } from '@angular/core';
|
||||
|
||||
import { FileUploader, FileUploaderOptions, FilterFunction } from './file-uploader.class';
|
||||
import { FileUploader, FileUploaderOptions } from './file-uploader.class';
|
||||
|
||||
@Directive({ selector: '[ng2FileDrop]' })
|
||||
export class FileDropDirective {
|
||||
@Input() public uploader: FileUploader;
|
||||
// tslint:disable-next-line:no-input-rename
|
||||
@Input('ng2FileFilter') public filter: FilterFunction['fn'];
|
||||
@Output() public fileOver: EventEmitter<any> = new EventEmitter();
|
||||
@Output() public onFileDrop: EventEmitter<FileList> = new EventEmitter<FileList>();
|
||||
@Output() public onFileDrop: EventEmitter<File[]> = new EventEmitter<File[]>();
|
||||
|
||||
protected element: ElementRef;
|
||||
|
||||
@@ -32,10 +30,7 @@ export class FileDropDirective {
|
||||
}
|
||||
|
||||
let options = this.getOptions();
|
||||
let filters = typeof this.filter === 'function' ? [{
|
||||
name: 'ng2FileDropDirectiveFilter',
|
||||
fn: this.filter
|
||||
}, ...options.filters] : this.getFilters();
|
||||
let filters = this.getFilters();
|
||||
this._preventAndStop(event);
|
||||
this.uploader.addToQueue(transfer.files, options, filters);
|
||||
this.fileOver.emit(false);
|
||||
@@ -66,11 +61,11 @@ export class FileDropDirective {
|
||||
this.fileOver.emit(false);
|
||||
}
|
||||
|
||||
protected _getTransfer(event: any): DragEvent['dataTransfer'] {
|
||||
protected _getTransfer(event: any): any {
|
||||
return event.dataTransfer ? event.dataTransfer : event.originalEvent.dataTransfer; // jQuery fix;
|
||||
}
|
||||
|
||||
protected _preventAndStop(event: Event): any {
|
||||
protected _preventAndStop(event: any): any {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import { Directive, ElementRef, EventEmitter, HostListener, Input, Output } from '@angular/core';
|
||||
import { Directive, EventEmitter, ElementRef, Input, HostListener, Output } from '@angular/core';
|
||||
|
||||
import { FileUploader, FilterFunction } from './file-uploader.class';
|
||||
import { FileUploader } from './file-uploader.class';
|
||||
|
||||
@Directive({ selector: '[ng2FileSelect]' })
|
||||
export class FileSelectDirective {
|
||||
@Input() public uploader: FileUploader;
|
||||
// tslint:disable-next-line:no-input-rename
|
||||
@Input('ng2FileFilter') public filter: FilterFunction['fn'];
|
||||
@Output() public onFileSelected: EventEmitter<File[]> = new EventEmitter<File[]>();
|
||||
|
||||
protected element: ElementRef;
|
||||
@@ -31,10 +29,7 @@ export class FileSelectDirective {
|
||||
public onChange(): any {
|
||||
let files = this.element.nativeElement.files;
|
||||
let options = this.getOptions();
|
||||
let filters = typeof this.filter === 'function' ? [{
|
||||
name: 'ng2FileSelectDirectiveFilter',
|
||||
fn: this.filter
|
||||
}, ...options.filters] : this.getFilters();
|
||||
let filters = this.getFilters();
|
||||
|
||||
this.uploader.addToQueue(files, options, filters);
|
||||
this.onFileSelected.emit(files);
|
||||
|
||||
@@ -14,9 +14,9 @@ export interface Headers {
|
||||
|
||||
export type ParsedResponseHeaders = { [ headerFieldName: string ]: string };
|
||||
|
||||
export interface FilterFunction {
|
||||
name: string;
|
||||
fn(this: FileUploader, item?: FileLikeObject, options?: FileUploaderOptions, queueIndex?: number): boolean | Promise<boolean>;
|
||||
export type FilterFunction = {
|
||||
name: string,
|
||||
fn: (item?: FileLikeObject, options?: FileUploaderOptions) => boolean
|
||||
};
|
||||
|
||||
export interface FileUploaderOptions {
|
||||
@@ -89,30 +89,26 @@ export class FileUploader {
|
||||
this.options.filters.unshift({ name: 'mimeType', fn: this._mimeTypeFilter });
|
||||
}
|
||||
|
||||
for (const q of this.queue) {
|
||||
q.url = this.options.url;
|
||||
for (let i = 0; i < this.queue.length; i++) {
|
||||
this.queue[ i ].url = this.options.url;
|
||||
}
|
||||
}
|
||||
|
||||
public async addToQueue(files: FileList, options?: FileUploaderOptions, filters?: FilterFunction[] | string): Promise<void> {
|
||||
public addToQueue(files: File[], options?: FileUploaderOptions, filters?: FilterFunction[] | string): void {
|
||||
let list: File[] = [];
|
||||
// tslint:disable-next-line:prefer-for-of
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
list.push(files[i]);
|
||||
for (let file of files) {
|
||||
list.push(file);
|
||||
}
|
||||
|
||||
let arrayOfFilters = this._getFilters(filters);
|
||||
let count = this.queue.length;
|
||||
let addedFileItems: FileItem[] = [];
|
||||
let idx = 0;
|
||||
|
||||
for (const some of list) {
|
||||
list.map((some: File) => {
|
||||
if (!options) {
|
||||
options = this.options;
|
||||
}
|
||||
|
||||
let temp = new FileLikeObject(some);
|
||||
if (await this._isValidFile(temp, arrayOfFilters, options, idx++)) {
|
||||
if (this._isValidFile(temp, arrayOfFilters, options)) {
|
||||
let fileItem = new FileItem(this, some, options);
|
||||
addedFileItems.push(fileItem);
|
||||
this.queue.push(fileItem);
|
||||
@@ -121,7 +117,7 @@ export class FileUploader {
|
||||
let filter = arrayOfFilters[ this._failFilterIndex ];
|
||||
this._onWhenAddingFileFailed(temp, filter, options);
|
||||
}
|
||||
}
|
||||
});
|
||||
if (this.queue.length !== count) {
|
||||
this._onAfterAddingAll(addedFileItems);
|
||||
this.progress = this._getTotalProgress();
|
||||
@@ -375,11 +371,11 @@ export class FileUploader {
|
||||
if (this.authToken) {
|
||||
xhr.setRequestHeader(this.authTokenHeader, this.authToken);
|
||||
}
|
||||
xhr.onreadystatechange = function (): void {
|
||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||
that.response.emit(xhr.responseText);
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState == XMLHttpRequest.DONE) {
|
||||
that.response.emit(xhr.responseText)
|
||||
}
|
||||
}
|
||||
};
|
||||
if (this.options.formatDataFunctionIsAsync) {
|
||||
sendable.then(
|
||||
(result: any) => xhr.send(JSON.stringify(result))
|
||||
@@ -424,18 +420,12 @@ export class FileUploader {
|
||||
return this.options.queueLimit === undefined || this.queue.length < this.options.queueLimit;
|
||||
}
|
||||
|
||||
protected async _isValidFile(file: FileLikeObject, filters: FilterFunction[], options: FileUploaderOptions, queueIndex: number): Promise<boolean> {
|
||||
protected _isValidFile(file: FileLikeObject, filters: FilterFunction[], options: FileUploaderOptions): boolean {
|
||||
this._failFilterIndex = -1;
|
||||
|
||||
for (const filter of filters) {
|
||||
return !filters.length ? true : filters.every((filter: FilterFunction) => {
|
||||
this._failFilterIndex++;
|
||||
|
||||
if (!(await Promise.resolve(filter.fn.call(this, file, options, queueIndex)))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return filter.fn.call(this, file, options);
|
||||
});
|
||||
}
|
||||
|
||||
protected _isSuccessCode(status: number): boolean {
|
||||
|
||||
Reference in New Issue
Block a user