0.0.6 • Published 6 years ago

angular-http-file-upload v0.0.6

Weekly downloads
6
License
MIT
Repository
github
Last release
6 years ago

angular-http-file-upload

Library to support HTTP file uploads for Angular (tested with Angular 5).

This is extension of https://github.com/j-zeng/angular2-http-file-upload to support Angular 5.

Getting Started

npm install angular-http-file-upload@latest --save

Add file upload service to your project

// app.module.ts

import { NgModule }      from '@angular/core';
import { UploaderModule }      from 'angular-http-file-upload';

@NgModule({
    // your module meta data here...
    imports: [ UploaderModule.forRoot() ]
})
export class AppModule { }

Set up a file upload item

// my-upload-item.ts

import { UploadItem }    from 'angular-http-file-upload';

export class MyUploadItem extends UploadItem {
    constructor(file: any) {
        super();
        this.url = 'https://your.domain.here/your.endpoint';
        this.headers = { HeaderName: 'Header Value', AnotherHeaderName: 'Another Header Value' };
        this.file = file;
    }
}

Use the file upload service in a component

// example.component.ts

import { Component }     from '@angular/core';
import { Uploader }      from 'angular-http-file-upload';
import { MyUploadItem }  from './my-upload-item';

@Component({
    // your component meta data here...
})
export class ExampleComponent {
    constructor(public uploaderService: Uploader) { }

    submit() {
        let uploadFile = (<HTMLInputElement>window.document.getElementById('myFileInputField')).files[0];

        let myUploadItem = new MyUploadItem(uploadFile);
        myUploadItem.formData = { FormDataKey: 'Form Data Value' };  // (optional) form data can be sent with file

        this.uploaderService.onSuccessUpload = (item, response, status, headers) => {
             // success callback
        };
        this.uploaderService.onErrorUpload = (item, response, status, headers) => {
             // error callback
        };
        this.uploaderService.onCompleteUpload = (item, response, status, headers) => {
             // complete callback, called regardless of success or failure
        };
        this.uploaderService.upload(myUploadItem);
    }
}

An upload progress callback is also available:

        this.uploaderService.onProgressUpload = (item, percentComplete) => {
             // progress callback
        };

License

MIT © Venkaiah Chowdary Koneru

0.0.6

6 years ago

0.0.6-beta.1

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago