1.0.1 • Published 7 years ago

angular4-file-submit v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

A simple library to deal with file submitting.

How to use

Import library to your module

import {FileSubmitModule} from "angular4-file-submit";

@NgModule({
    ],
    imports: [
        FileSubmitModule
    ]
})
export class AdminModule {
}

After that, you are free to use directive for you inputs.

<input type="file" name="image_file" ng4-file-submit>

Directive provides a public method to get formData object ready to be submitted with your service

import {Component, Input, OnInit, ViewChild} from '@angular/core';
import {MdDialogRef} from "@angular/material";
import {PhotosService} from "../../../../services/photos.service";
import {Photo, CATEGORIES_LIST} from "../../../../../shared/models/photo.model";
import {FileSubmitDirective} from 'angular4-file-submit';

@Component({
    selector: 'app-upload-photo',
    templateUrl: 'upload-photo.component.html',
    styleUrls: [
        'upload-photo.component.scss'
    ]
})
export class UploadPhotoComponent implements OnInit {
    @ViewChild(FileSubmitDirective) fileSubmitDirective: FileSubmitDirective;

    
    uploadPhoto() {
        if (!this.validateModel()) {
            return;
        }

        let formData = this.fileSubmitDirective.getFormData();

        for (const prop in this.model) {
            formData.set(prop, this.model[prop]);
        }

        return this.photosService.createPhoto(formData)
            .then((results) => {
                this.dialogRef.close(results[0]);
            });
    }
}
1.0.1

7 years ago

1.0.0

7 years ago

0.1.0

7 years ago