Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with setValue on FileInput control #9

Closed
Amunds opened this issue Jul 22, 2018 · 7 comments
Closed

Error with setValue on FileInput control #9

Amunds opened this issue Jul 22, 2018 · 7 comments
Labels
bug Something isn't working

Comments

@Amunds
Copy link

Amunds commented Jul 22, 2018

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.

@merlosy
Copy link
Owner

merlosy commented 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();

@Amunds
Copy link
Author

Amunds commented Jul 23, 2018

I tried adding new FileInput([reader.result]) but got the same error. I've also started getting NaN for actualSize in the validator. However, the validator is no longer throwing TypeError.

I'm now using the readFile function with the same issue. Please correct me if I'm wrong as I'm not proficient enough to debug this myself, but from my understanding FileInput only accepts File and not Blob. So when I overwrite the value of with a blob it causes errors because it can't find fileName and fileSize etc.

So if I want to save a base64 blob to the database I'd instead want to populate a hidden field instead of trying to set the value on the , correct?

@merlosy
Copy link
Owner

merlosy commented Jul 24, 2018

Can you manage to set up a stackblitz demo of your issue.
That would be easier for me to debug your issue.

You can fork this: https://stackblitz.com/edit/ngx-material-file-input

@Amunds
Copy link
Author

Amunds commented Jul 24, 2018

Here's the stackblitz: https://ngx-material-file-input-v3a7ij.stackblitz.io

@merlosy
Copy link
Owner

merlosy commented Jul 25, 2018

Ok, this seems to be related to template driven forms.
https://stackblitz.com/edit/ngx-material-file-input-v3a7ij

I'll add to my todo list

@merlosy merlosy added the bug Something isn't working label Jul 25, 2018
@peterbsmyth
Copy link

@Amunds I used this to get around the same error you had

@merlosy
Copy link
Owner

merlosy commented Sep 16, 2018

closing as of version 0.3.0

@merlosy merlosy closed this as completed Sep 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants