7

Good day,

How can i handle input type file in formControl? im using reactive form but when i get the value of my form it returns null value on my <input type="file">??

2
  • what do you expect to see there? and post your full code Commented Jul 21, 2017 at 7:19
  • the file path, so i can push it on my array, and store it on objects. Commented Jul 21, 2017 at 7:50

2 Answers 2

13

You need to write your own FileInputValueAccessor. Here is the plunker and the code:

@Directive({
  selector: 'input[type=file]',
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: FileValueAccessorDirective,
      multi: true
    }
  ]
})
export class FileValueAccessorDirective implements ControlValueAccessor {
  onChange;

  @HostListener('change', ['$event.target.value']) _handleInput(event) {
    this.onChange(event);
  }

  constructor(private element: ElementRef, private render: Renderer2) {  }

  writeValue(value: any) {
    const normalizedValue = value == null ? '' : value;
    this.render.setProperty(this.element.nativeElement, 'value', normalizedValue);
  }

  registerOnChange(fn) {    this.onChange = fn;  }

  registerOnTouched(fn: any) {  }

  nOnDestroy() {  }
}

And then you will be able to get updates like this:

@Component({
  moduleId: module.id,
  selector: 'my-app',
  template: `
      <h1>Hello {{name}}</h1>
      <h3>File path is: {{path}}</h3>
      <input type="file" [formControl]="ctrl">
  `
})
export class AppComponent {
  name = 'Angular';
  path = '';
  ctrl = new FormControl('');

  ngOnInit() {
    this.ctrl.valueChanges.subscribe((v) => {
      this.path = v;
    });
  }
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks Maximus, but how can i get the full path of the file? in your example you get the file path with C:\fakepath\sample.xls is their a way on getting the full path file? im trying to get the file path so i can send it on the server.
@JydonMah, what do you mean full file path? This is the absolute file path
just for example my file is located at 'C:\documents\sample.xls' it should return the C:\documents\sample.xls and not the C:\fakepath\sample.xls.
unfortunately, a browser doesn't expose that information because of security reasons. See here. The only thing you can get is fileName
Thanks Maximus for your example and info ;) finally understand it. many thanks.
|
1

there is no way to handle this in angular form-control. we can provide some hack to make this work if you want to upload the image. just add the <input type=file> as form control on which user can add the file and acter grabbing the file we can change it to the base64code and then assign that value to the hidden field of our main form. then we can store the image file in that way.

else you can go for the ng-file-upload moduleng file upload

3 Comments

thanks for this advice, ill try to use local reference and pass value on it, but how can we get the full path of file after grabing that value? im getting fakepath everytime i get the value of input files
you can implement your own value accessor to specific input types, see my answer
great solution we can go for this as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.