Initial value of input type file from a FormControl

2

When I try to add an initial value to an input type file from FormControl I get the following error:

  

ERROR DOMException: Failed to set the 'value' property on   'HTMLInputElement': This input element accepts a filename, which may   only be programmatically set to the empty string.

This is the code of my FormGroup :

this.editProduct = new FormGroup({
    'productImage' : new FormControl( '', Validators.required )
});

ngOnInit() {
    this._productsService.getProduct( this.productId )
          .subscribe( getProductData => {
    this.editProduct.controls['productImage'].setValue( 
        this.product.productImage );
    }
}

HTML Form:

<form class="animated fadeIn" [formGroup]="editProduct" 
(ngSubmit)="updateProduct()" #form="ngForm">
<input class="form-control" type="file" [ngClass]="{ 'is-invalid' : 
!editProduct.get( 'productImage' ).valid }" id="productImage" 
formControlName="productImage">
    <button class="btn btn-primary" type="submit" 
    [disabled]="!form.valid">Actualizar</button>
</form>
    
asked by Raúl Mario Pozo Henríquez 08.10.2018 в 14:42
source

1 answer

5

The error is quite explanatory: In inputs of type "file" you can not define an initial value other than "" (empty string).

It's not about AngularJS, it's that no browser will allow you to define a default path because it would be a huge security hole: you could create a malicious page that would take a file, open it from the browser and send all the information by AJAX to my server

    
answered by 08.10.2018 / 15:06
source