Home > Software design >  vue file input set default value when v-model is not allowed
vue file input set default value when v-model is not allowed

Time:10-08

Maybe someone gives me advise how to set default value for input type files in vue (i have file name, but I think I need something more)? I know that v-model is senseless, so I have no idea how to do it, and can't found some solutions.

CodePudding user response:

You cannot set default value to file inputs. You just display file info in separate element and use file input just to upload new files. E.g.

<div>
    {{file.name}}
    <!-- Just display the name of the existing file -->

    <button @click.prevent="removeFile(file)">Remove</button>
    <!-- button to delete existing file -->
</div>

<input type="file" @change="handleUpload"> 
<!-- Show input just to upload new file and replace the old one -->

CodePudding user response:

Thank you for your answers. Then I try to use v-model there I have error v-model is not allowed here. To be honest there was problem with validation during edit. File input is empty, because I can't preaload data, and validation not allowed to send data to backend.

In the end I decided that when there are edit, file input not need to be required, because object get information about ealier choosen file, and you can change it, but can't remove.

  • Related