Today I was integrated ng2-file-upload package to my angular 6 application, It was pretty simple, Please find the below high-level code.
import the ng2-file-upload module
app.module.ts
import { FileUploadModule } from 'ng2-file-upload';
------
------
imports: [ FileUploadModule ],
------
------
Component ts file import FileUploader
app.component.ts
import { FileUploader, FileLikeObject } from 'ng2-file-upload';
------
------
const URL = 'http://localhost:3000/fileupload/';
------
------
public uploader: FileUploader = new FileUploader({
url: URL,
disableMultipart : false,
autoUpload: true,
method: 'post',
itemAlias: 'attachment'
});
public onFileSelected(event: EventEmitter<File[]>) {
const file: File = event[0];
console.log(file);
}
------
------
Component HTML add file tag
app.component.html
<input type="file" #fileInput ng2FileSelect [uploader]="uploader" (onFileSelected)="onFileSelected($event)" />
Working Online stackblitz Link: https://ng2-file-upload-example.stackblitz.io
Stackblitz Code example: https://stackblitz.com/edit/ng2-file-upload-example
Official documentation link https://valor-software.com/ng2-file-upload/