nixa's avatar
Level 1

Livewire - File Upload not working

Wondering if I could get some insight on why the file upload function may not be working for me.

I have a modal form with a file upload section. When I select a file, the file name is filled out; however, if I go back and change another field in the form, the file upload section gets changed back to"no file chosen".

If I reselect the image file to upload and click the Create button, it states that the "The file must be an image.".

The file seems to never upload to the server. When I peek at the livewire instance in Google Chrome, the variable I'm referencing for the file input is still null.

I am including:

    @livewireStyles

In the head tag

and:

    @livewireScripts

in the end of the body tag.

Below are my code snippets:

Livewire Controller

use Livewire\WithFileUploads;
...
class AddImage extends Component
{
    use WithFileUploads;
     $this->markerImage = null;
    protected $rules = [
        'markerImage' => 'image|mimes:png|dimensions:max_width=100,max_height=100|max:4096'
    ];
....
    public function addMapMarker()
    {
        $this->validate();
        dd("SUCCESS");
     }
}

Blade Snippet:

                                <div class="row mb-2">
                                        @if ($markerImage)
                                            Photo Preview:
                                            <img src="{{ $markerImage->temporaryUrl() }}">
                                        @endif
                                        <div class="form-group p-2">
                                            <label for="markerImage" class="form-label">Marker Image</label>
                                            <input type="file" wired:model="markerImage">
                                            <div wire:loading wire:target="photo">Uploading...</div>
                                            <small>Max dimensions 100 px (W) x 100 px (H). Image must be of a .png file type</small>
                                            @if($errors->has('markerImage'))
                                                <div class="col p-3 my-2 bg-danger dw-rounded">
                                                    <strong>{{ $errors->first('markerImage') }}</strong>
                                                </div>
                                            @endif
                                    </div>
                                </div>

                                    <button type="button" class="btn dw-yellow dw-rounded text-dark" wire:click="addMapMarker">
                                        <strong>Create</strong>
                                    </button>

There are no errors in the laravel log. I'm a bit confused on why it's behaving like this.

Thanks!

0 likes
4 replies
ericknyoto's avatar

Try remove the default null of $this->markerImage, and add required, to the rules validation.

nixa's avatar
Level 1

An odd behavior I'm noticing is that when I select an image to upload, it is not sending an initial request back to the server to populate the variable (and get the temporaryURL).

No events are being triggered when I initially select the file to be uploaded.

Also, let's say I opt to select an image file first and then fill out the rest of the form, the File Upload section that normally has the filename will revert back to "no file chosen".

nixa's avatar
nixa
OP
Best Answer
Level 1

Well I figured out my issue! Gah... it was a tricky one.

I did:

<input type="file" wired:model="markerImage">

Instead of:

<input type="file" wire:model="markerImage">

I used wired instead of wire.

DOH!

1 like

Please or to participate in this conversation.