ngx-uploader
Angular 2+ File Uploader
http://ngx-uploader.com
Got a Question or Problem?
Do not open issues for general support questions as we want to keep GitHub issues for bug reports and feature requests. You've got much better chances of getting your question answered on Stack Overflow where the questions should be tagged with tag ngx-uploader
.
To save your and our time, we will systematically close all issues that are requests for general support and redirect people to Stack Overflow.
Installation
- Add
ngx-uploader
module as dependency to your project.
npm install ngx-uploader --save
- Include
NgxUploaderModule
into your main AppModule or in module where you will use it.
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { NgxUploaderModule } from 'ngx-uploader';
@NgModule({
imports: [BrowserModule, NgxUploaderModule],
declarations: [AppComponent]
})
export class AppModule {}
or include NgxUploaderModule
into your SharedModule. This could be usefull if your project has nested Modules.
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NgxUploaderModule } from 'ngx-uploader';
...
@NgModule({
imports: [
CommonModule,
NgxUploaderModule,
...
],
exports: [
CommonModule,
NgxUploaderModule,
...
],
...
})
export class SharedModule {
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { SharedModule } from './shared.module';
@NgModule({
imports: [BrowserModule, SharedModule],
declarations: [AppComponent]
})
export class AppModule {}
Data Structures of Events and Uploaded Files
export interface UploaderOptions {
concurrency: number;
allowedContentTypes?: string[];
maxUploads?: number;
maxFileSize?: number;
}
export interface UploadProgress {
status: UploadStatus;
data?: {
percentage: number;
speed: number;
speedHuman: string;
eta: number;
etaHuman: string;
};
}
export interface UploadFile {
id: string;
fileIndex: number;
lastModifiedDate: Date;
name: string;
size: number;
type: string;
form: FormData;
progress: UploadProgress;
response?: any;
responseStatus?: number;
responseHeaders?: { [key: string]: string };
}
export interface UploadOutput {
type:
| 'addedToQueue'
| 'allAddedToQueue'
| 'uploading'
| 'done'
| 'removed'
| 'start'
| 'cancelled'
| 'dragOver'
| 'dragOut'
| 'drop';
file?: UploadFile;
nativeFile?: File;
}
export interface UploadInput {
type: 'uploadAll' | 'uploadFile' | 'cancel' | 'cancelAll' | 'remove' | 'removeAll';
url?: string;
method?: string;
id?: string;
fieldName?: string;
fileIndex?: number;
file?: UploadFile;
data?: { [key: string]: string | Blob };
headers?: { [key: string]: string };
includeWebKitFormBoundary?: boolean;
concurrency?: number;
withCredentials?: boolean;
}
Upload Restrictions
With version 4.2.1 we've introduced restrictions for Content-Types.
To not break the behaviour of previous releases, there are no restrictions by default at all.
Look at app-home.component.ts for an example of Content-Type restriction.
If you want to toast a message for the rejected file, add this to your onUploadOutput method.
onUploadOutput(output: UploadOutput): void {
....
} else if (output.type === 'rejected' && typeof output.file !== 'undefined') {
}
...
Token Authorization
If you have to upload files with Token Authorization, you can set the header in startUpload as follows.
startUpload(): void {
let token = this.myToken;
const event: UploadInput = {
type: 'uploadAll',
url: 'http://ngx-uploader.com/upload',
method: 'POST',
headers: { 'Authorization': 'JWT ' + token },
data: { foo: 'bar' },
includeWebKitFormBoundary: true
};
this.uploadInput.emit(event);
}
Example
You can always run working example by cloning this repository, building project with yarn build:prod
and running server with node ./dist-app/api/index.js
.
Component Code
import { Component, EventEmitter } from '@angular/core';
import { UploadOutput, UploadInput, UploadFile, humanizeBytes, UploaderOptions } from 'ngx-uploader';
@Component({
selector: 'app-home',
templateUrl: 'app-home.component.html'
})
export class AppHomeComponent {
options: UploaderOptions;
formData: FormData;
files: UploadFile[];
uploadInput: EventEmitter<UploadInput>;
humanizeBytes: Function;
dragOver: boolean;
constructor() {
this.options = { concurrency: 1, maxUploads: 3, maxFileSize: 1000000 };
this.files = [];
this.uploadInput = new EventEmitter<UploadInput>();
this.humanizeBytes = humanizeBytes;
}
onUploadOutput(output: UploadOutput): void {
switch (output.type) {
case 'allAddedToQueue':
break;
case 'addedToQueue':
if (typeof output.file !== 'undefined') {
this.files.push(output.file);
}
break;
case 'uploading':
if (typeof output.file !== 'undefined') {
const index = this.files.findIndex(file => typeof output.file !== 'undefined' && file.id === output.file.id);
this.files[index] = output.file;
}
break;
case 'removed':
this.files = this.files.filter((file: UploadFile) => file !== output.file);
break;
case 'dragOver':
this.dragOver = true;
break;
case 'dragOut':
case 'drop':
this.dragOver = false;
break;
case 'done':
break;
}
}
startUpload(): void {
const event: UploadInput = {
type: 'uploadAll',
url: 'http://ngx-uploader.com/upload',
method: 'POST',
data: { foo: 'bar' }
};
this.uploadInput.emit(event);
}
cancelUpload(id: string): void {
this.uploadInput.emit({ type: 'cancel', id: id });
}
removeFile(id: string): void {
this.uploadInput.emit({ type: 'remove', id: id });
}
removeAllFiles(): void {
this.uploadInput.emit({ type: 'removeAll' });
}
}
Template Code
For whole template code please check here.
<div
class="drop-container"
ngFileDrop
[options]="options"
(uploadOutput)="onUploadOutput($event)"
[uploadInput]="uploadInput"
[ngClass]="{ 'is-drop-over': dragOver }"
>
<h1>Drag & Drop</h1>
</div>
<label class="upload-button">
<input
type="file"
ngFileSelect
[options]="options"
(uploadOutput)="onUploadOutput($event)"
[uploadInput]="uploadInput"
multiple
/>
or choose file(s)
</label>
<button type="button" class="start-upload-btn" (click)="startUpload()">Start Upload</button>
LICENCE
MIT