Form File
Single File Mode
The default behavior is single file mode. While using single file mode the modelValue
will be a single File
object
<template>
<BFormFile v-model="file" label="Hello!" />
<div class="mt-3">
Files: <strong>{{ file }}</strong>
</div>
</template>
<script setup lang="ts">
const file = ref<null | File>(null)
</script>
Multiple File Mode
To toggle multiple file mode, simply set the multiple
prop to true
. While in multiple file mode, the modelValue
will be a File[]
, even if only one file is selected
<template>
<BFormFile v-model="files" multiple />
<div class="mt-3">
Files: <strong>{{ files }}</strong>
</div>
</template>
<script setup lang="ts">
const files = ref<null | File[]>(null)
</script>
Limiting to certain file types
You can limit the file types by setting the accept
prop. The accept
attribute is a csv list of acceptable types. This can be a string
or string[]
. If a string[]
is inputted, it simply gets joined as a csv list
<template>
<BFormFile v-model="file" accept="image/*" />
<div class="mt-3">
Files: <strong>{{ file }}</strong>
</div>
</template>
<script setup lang="ts">
const file = ref<null | File>(null)
</script>
Drag and Drop Support
Drag and drop support uses the browsers default behavior. You can explicitly disable drag and drop by using the noDrop
prop
<template>
<BFormFile v-model="file" no-drop />
<div class="mt-3">
Files: <strong>{{ file }}</strong>
</div>
</template>
<script setup lang="ts">
const file = ref<null | File>(null)
</script>
Sizing
You can modify the size of the form control by using the size
prop
<BFormFile class="mt-3" size="sm" />
<BFormFile class="mt-3" />
<BFormFile class="mt-3" size="lg" />
Label
You can add a label above the input by using the label
prop or the label
slot
<BFormFile class="mt-3" label="I am first!" />
<BFormFile class="mt-3">
<template #label>
I am second!
</template>
</BFormFile>
Directory Mode
By adding the directory
prop, a user can select directories instead of files
Example to be Written
Autofocus
If you set the autofocus
prop to true, the input will be focused when the component is inserted
<BFormFile class="mt-3" autofocus />
Contextual State
You can use the state
prop to provide visual feedback on the state of the input
<BFormFile class="mt-3" :state="false" />
<BFormFile class="mt-3" :state="true" />
Modifying the file selection
With inputs that are of type file
, the value is strictly uni-directional
. Meaning that you cannot change the value of the input via JavaScript. You can change the value of the v-model
, and this will work for an "outside view", however, the actual input
element will not have its FileList changed. This is for security reasons as a malicious script could attempt to read and steal documents
Exposed functions
The BFormFile exposes functions to control the component: focus(), blur(), reset()
. These are accessed through the template ref.
- Focus: focuses the file input
- Blur: blurs the file input focus
- Reset: Resets the file selection so that no file is selected
Component Reference
<BFormFile>
Prop | Type | Default | Description |
---|---|---|---|
accept | string | string[] | '' | Value to set on the file input's `accept` attribute |
aria-label | string | undefined | Sets the value of `aria-label` attribute on the rendered element |
aria-labelledby | string | undefined | The ID of the element that provides a label for this component. Used as the value for the `aria-labelledby` attribute |
autofocus | boolean | false | When set to `true`, attempts to auto-focus the control when it is mounted, or re-activated when in a keep-alive. Does not set the `autofocus` attribute on the control |
capture | 'boolean' | 'user' | 'environment' | false | When set, will instruction the browser to use the devices camera (if supported) |
directory | boolean | false | Enable `directory` mode (on browsers that support it) |
disabled | boolean | false | When set to `true`, disables the component's functionality and places it in a disabled state |
form | string | undefined | ID of the form that the form control belongs to. Sets the `form` attribute on the control |
id | string | undefined | Used to set the `id` attribute on the rendered content, and used as the base to generate any additional element IDs as needed |
label | string | '' | Sets the label for the form group which the file input is rendered |
label-class | ClassValue | undefined | Sets the styling for the label |
model-value | File[] | File | null | undefined | The current value of the file input. Will be a single `File` object or an array of `File` objects (if `multiple` or `directory` is set). Can be set to `null`, or an empty array to reset the file input |
multiple | boolean | false | When set, will allow multiple files to be selected. `v-model` will be an array |
name | string | undefined | Sets the value of the `name` attribute on the form control |
no-button | boolean | null | undefined | hide the file input button |
no-drop | boolean | false | Disable drag and drop mode |
no-traverse | boolean | false | Wether to returns files as a flat array when in `directory` mode |
plain | boolean | false | Don't add any additional styling or classes to the file input |
required | boolean | undefined | Adds the `required` attribute to the form control |
size | Size | 'md' | Set the size of the component's appearance. 'sm', 'md' (default), or 'lg' |
state | ValidationState | undefined | Controls the validation state appearance of the component. `true` for valid, `false` for invalid, or `null` for no validation state |
Event | Args | Description |
---|---|---|
change | value : Event - The browsers default change event | Original change event of the input |
update:model-value | value : File | File[] | null - Will be a single File object in single mode or an array of File objects in multiple mode | Updates the `v-model` value (see docs for more details) |
Name | Scope | Description |
---|---|---|
label |