Skip to content

Commit

Permalink
New beforeUpload event - makes it possible to validate files before t…
Browse files Browse the repository at this point in the history
…he upload starts (#142)

beforeUpload event - validate file (size) before upload starts
  • Loading branch information
eivindr authored and jkuri committed Dec 1, 2016
1 parent b254c27 commit f9c6cc0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class DemoApp {
options: Object = {
url: 'http://localhost:10050/upload'
};
sizeLimit = 2000000;

handleUpload(data): void {
if (data && data.response) {
Expand All @@ -76,6 +77,13 @@ export class DemoApp {
fileOverBase(e:any):void {
this.hasBaseDropZoneOver = e;
}

beforeUpload(uploadingFile): void {
if (uploadingFile.size > this.sizeLimit) {
uploadingFile.setAbort();
alert('File is too large');
}
}
}
````

Expand All @@ -84,7 +92,8 @@ export class DemoApp {
<input type="file"
ngFileSelect
[options]="options"
(onUpload)="handleUpload($event)">
(onUpload)="handleUpload($event)"
(beforeUpload)="beforeUpload($event)">

<!-- drag & drop file example-->
<style>
Expand All @@ -94,7 +103,8 @@ export class DemoApp {
[options]="options"
(onUpload)="handleUpload($event)"
[ngClass]="{'file-over': hasBaseDropZoneOver}"
(onFileOver)="fileOverBase($event)">
(onFileOver)="fileOverBase($event)"
(beforeUpload)="beforeUpload($event)">
</div>

<div>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ng2-uploader",
"description": "Angular2 File Uploader",
"version": "1.5.3",
"version": "1.5.4",
"license": "MIT",
"main": "ng2-uploader.js",
"typings": "ng2-uploader.d.ts",
Expand Down
7 changes: 6 additions & 1 deletion src/directives/ng-file-drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Output,
HostListener
} from '@angular/core';
import { Ng2Uploader, UploadRejected } from '../services/ng2-uploader';
import { Ng2Uploader, UploadRejected, UploadedFile } from '../services/ng2-uploader';

@Directive({
selector: '[ngFileDrop]'
Expand All @@ -18,6 +18,7 @@ export class NgFileDropDirective {
@Output() onPreviewData: EventEmitter<any> = new EventEmitter();
@Output() onFileOver:EventEmitter<any> = new EventEmitter();
@Output() onUploadRejected: EventEmitter<UploadRejected> = new EventEmitter<UploadRejected>();
@Output() beforeUpload: EventEmitter<UploadedFile> = new EventEmitter<UploadedFile>();

_options:any;

Expand Down Expand Up @@ -51,6 +52,10 @@ export class NgFileDropDirective {
this.onPreviewData.emit(data);
});

this.uploader._beforeEmitter.subscribe((uploadingFile: UploadedFile) => {
this.beforeUpload.emit(uploadingFile)
});

setTimeout(() => {
if (this.events instanceof EventEmitter) {
this.events.subscribe((data: string) => {
Expand Down
7 changes: 6 additions & 1 deletion src/directives/ng-file-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Output,
HostListener
} from '@angular/core';
import { Ng2Uploader, UploadRejected } from '../services/ng2-uploader';
import { Ng2Uploader, UploadRejected, UploadedFile } from '../services/ng2-uploader';

@Directive({
selector: '[ngFileSelect]'
Expand All @@ -17,6 +17,7 @@ export class NgFileSelectDirective {
@Output() onUpload: EventEmitter<any> = new EventEmitter();
@Output() onPreviewData: EventEmitter<any> = new EventEmitter();
@Output() onUploadRejected: EventEmitter<UploadRejected> = new EventEmitter<UploadRejected>();
@Output() beforeUpload: EventEmitter<UploadedFile> = new EventEmitter<UploadedFile>();

_options: any;

Expand Down Expand Up @@ -53,6 +54,10 @@ export class NgFileSelectDirective {
this.onPreviewData.emit(data);
});

this.uploader._beforeEmitter.subscribe((uploadingFile: UploadedFile) => {
this.beforeUpload.emit(uploadingFile)
});

setTimeout(() => {
if (this.events) {
this.events.subscribe((data: string) => {
Expand Down
9 changes: 8 additions & 1 deletion src/services/ng2-uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class Ng2Uploader {
_queue: any[] = [];
_emitter: EventEmitter<any> = new EventEmitter();
_previewEmitter: EventEmitter<any> = new EventEmitter();
_beforeEmitter: EventEmitter<UploadedFile> = new EventEmitter();

setOptions(options: any): void {
this.url = options.url != null ? options.url : this.url;
Expand Down Expand Up @@ -203,7 +204,13 @@ export class Ng2Uploader {
xhr.setRequestHeader('Authorization', `${this.authTokenPrefix} ${this.authToken}`);
}

xhr.send(form);
this._beforeEmitter.emit(uploadingFile);

if (!uploadingFile.abort) {
xhr.send(form);
} else {
this.removeFileFromQueue(queueIndex);
}
}

addFilesToQueue(files: File[]): void {
Expand Down

0 comments on commit f9c6cc0

Please sign in to comment.