question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error with setValue on FileInput control

See original GitHub issue

I’m doing a onChange: <ngx-mat-file-input (change)="onFileChange(formDoc.value)">

  onFileChange(event) {
    let reader = new FileReader();
    let file = event.requiredfile.files[0];
    reader.onload = () => {
      if(reader.result) {
        this.formDoc.get('photo').setValue(reader.result);
      }
    };
    reader.readAsDataURL(file);
  }

This results in the following error in console:

FileInputComponent.html:1 ERROR TypeError: this._files.map is not a function
    at new FileInput (file-input.model.ts:8)
    at FileInputComponent.get [as value] (file-input.component.ts:41)
    at FileInputComponent.get [as fileNames] (file-input.component.ts:172)
    at Object.eval [as updateRenderer] (FileInputComponent.html:2)
    at Object.debugUpdateRenderer [as updateRenderer] (core.js:10782)
    at checkAndUpdateView (core.js:10158)
    at callViewAction (core.js:10394)
    at execComponentViewsAction (core.js:10336)
    at checkAndUpdateView (core.js:10159)
    at callViewAction (core.js:10394)

The error happens as soon as onFileChange is called and continues forever as long as you’re on the page.

There is also an error with the validator:

ERROR TypeError: Cannot read property 'map' of undefined at file-validator.ts:15 at forms.js:461 at Array.map (<anonymous>) at _executeValidators (forms.js:461) at forms.js:434 at forms.js:461 at Array.map (<anonymous>) at _executeValidators (forms.js:461) at FormControl.validator (forms.js:434) at FormControl.push.../../node_modules/@angular/forms/fesm5/forms.js.AbstractControl._runValidator (forms.js:2228)

The code does what it’s supposed to do though, so it’s not breaking anything as far as I can tell. Tested with the demo app and the same happens there.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
merlosycommented, Jul 23, 2018

Hi Amunds, Form the docs, the value of the field should be a FileModel object. Implementation here: https://github.com/merlosy/ngx-material-file-input/blob/master/libs/material-file-input/src/lib/model/file-input.model.ts

You should try:

this.formDoc.get('photo').patchValue( new FileInput([reader.result]) );

Let me know if this solves your issue.

Bonus tip: you can try using fromEvent() observable to read data from you File/Blob (not tested)

readFile(file: File | Blob): Observable<any> {
    const reader = new FileReader();
    loadend = fromEvent(reader, 'loadend').pipe(
        map((read: any) => {
            return read.target.result;
        })
    );
    reader.readAsDataURL(file);
    return loadend;
}

// then

readFile(file).subscribe();
0reactions
merlosycommented, Sep 16, 2018

closing as of version 0.3.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't set the value of a file input with javascript - Stack Overflow
You are trying the set the value of the <input type="file"> to a string. This can't be done since type="file" only accepts file's...
Read more >
Controlled file input components in React - Medium
The problem​​ In React, an <input type="file" /> is always an uncontrolled component because its value can only be set by a user,...
Read more >
FileInput - Mantine
Capture files from user.
Read more >
Reset a File input in React | bobbyhadz
To reset a file input in React, set the input's value to null in your handleChange function, e.g. event.target.value = null . Setting...
Read more >
How to Implement File Uploading in Angular Reactive Forms
Creating a Custom Form Control. At this point, we have a problem. Angular doesn't come with a built-in value accessor for file input,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found