Alpine: x-model not working on file upload

Created on 2 Mar 2020  路  9Comments  路  Source: alpinejs/alpine

This is not working:

<div x-data="{ fileName: '' }">
  <input type="file" x-model="fileName">
  <span x-text="fileName"></span>
</div>

I have to use something like this instead:

<div x-data="{ fileName: '' }">
    <input type="file" x-ref="file" @change="fileName = $refs.file.value">
    <span x-text="fileName"></span>
</div>
enhancement

Most helpful comment

<div x-data="{ fileName: '' }">
    <input type="file" x-ref="file" @change="fileName = $refs.file.files[0].name">
    <p class="text-red-500" x-text="fileName" />
</div>

If you want to use multiple attribute, then you must use the for directive, because there arr not only one item in the files array

All 9 comments

One soruce of bad functionalty is the using of FileList. File input has an array of files

<div x-data="{ fileName: '' }">
    <input type="file" x-ref="file" @change="fileName = $refs.file.files[0].name">
    <p class="text-red-500" x-text="fileName" />
</div>

If you want to use multiple attribute, then you must use the for directive, because there arr not only one item in the files array

Thanks, but $refs.file.value works as well. The problem with x-model is I think the fact that you can get the value but you can't set it programatically. That's why it doesn't work.

Interesting, what does Vue do with v-model on a file input? I would say, we just mimick that

v-model doesn't seem to have any special behavior for file inputs.
Should we even have one? Do file inputs need two-way data binding? 馃

v-model doesn't seem to have any special behavior for file inputs.
Should we even have one? Do file inputs need two-way data binding? 馃

It could be useful in two ways:
1) As a shorthand for getting the value which is totally available
2) For resetting the file input, javascript can set the value to an empty string, it just can't set it to a new file address

Interesting, what does Vue do with v-model on a file input? I would say, we just mimick that

I don't think Vue has done a perfect job here. v-model is for getting any value, and setting any value, here is the situation with file inputs:
1) You can get any value
2) You can set the value to an empty string
3) You can't set the value to non-empty strings

I say we keep what we have access to, and just handle the case that causes a fatal error and show a warning in the console saying you can't set the file address on file inputs and give them a link to Mozilla or something.

hi @mokhosh
Can you give me more details about the issue?

I had a quick look and it's currently matching VueJS.

When you apply x-model to a input file you can:

  • read the value (it will be the path of that file on your disk)
  • set the value to '' (to reset the file input)
    You cannot:
  • set the value to an arbitrary string since you would get a DOM Exception (Vue and Vanilla JS would get the same exception).

Is it just about capturing the exception on the third case to allow the following directives to update?

Yes, tried it and seems like it's been fixed somewhere along the way.
Thanks.

Great news. Thanks for checking.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

haipham picture haipham  路  4Comments

mrmathewc picture mrmathewc  路  4Comments

imliam picture imliam  路  5Comments

BernhardBaumrock picture BernhardBaumrock  路  3Comments

hkan picture hkan  路  3Comments