import { Component , Input , OnInit } from '@angular/core' ; import { FormGroup } from '@angular/forms' ; @ Component ( { selector : 'cmdb-select-file-drag-drop' , templateUrl : './select-file-drag-drop.component.html' , styleUrls : [ './select-file-drag-drop.component.scss' ] } ) export class SelectFileDragDropComponent implements OnInit { @ Input ( ) formGroup : FormGroup ; public syntaxError : boolean = false ; ngOnInit ( ) { } uploadFile ( event ) { const file = event [ 0 ] ; const fileReader = new FileReader ( ) ; fileReader . readAsText ( file , 'UTF-8' ) ; fileReader . onload = ( ) => { if ( typeof fileReader . result === 'string' ) { this . syntaxError = false ; this . formGroup . get ( 'format' ) . setValue ( event [ 0 ] . type ) ; this . formGroup . get ( 'name' ) . setValue ( event [ 0 ] . name ) ; this . formGroup . get ( 'size' ) . setValue ( event [ 0 ] . size ) ; try { this . formGroup . get ( 'file' ) . setValue ( JSON . parse ( fileReader . result ) ) ; } catch ( err ) { console . log ( err ) ; this . syntaxError = true ; this . formGroup . get ( 'file' ) . setValue ( null ) ; } } } ; fileReader . onerror = ( error ) => { console . log ( error ) ; this . syntaxError = true ; } ; } }