Form File

File input control that supports single and multiple file modes

Single File Mode

The default behavior is single file mode. While using single file mode the modelValue will be a single File object

File:
HTML
vue
<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

Files:
HTML
vue
<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

File:
HTML
vue
<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

File:
HTML
vue
<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

HTML
template
<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

HTML
template
<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

HTML
template
<BFormFile class="mt-3" autofocus />

Contextual State

You can use the state prop to provide visual feedback on the state of the input

HTML
template
<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.

  1. Focus: focuses the file input
  2. Blur: blurs the file input focus
  3. Reset: Resets the file selection so that no file is selected

Component Reference

<BFormFile>
PropTypeDefaultDescription
acceptstring | string[]'' Value to set on the file input's `accept` attribute
aria-labelstringundefined Sets the value of `aria-label` attribute on the rendered element
aria-labelledbystringundefined The ID of the element that provides a label for this component. Used as the value for the `aria-labelledby` attribute
autofocusbooleanfalse 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)
directorybooleanfalse Enable `directory` mode (on browsers that support it)
disabledbooleanfalse When set to `true`, disables the component's functionality and places it in a disabled state
formstringundefined ID of the form that the form control belongs to. Sets the `form` attribute on the control
idstringundefined Used to set the `id` attribute on the rendered content, and used as the base to generate any additional element IDs as needed
labelstring'' Sets the label for the form group which the file input is rendered
label-classClassValueundefined Sets the styling for the label
model-valueFile[] | File | nullundefined 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
multiplebooleanfalse When set, will allow multiple files to be selected. `v-model` will be an array
namestringundefined Sets the value of the `name` attribute on the form control
no-buttonboolean | nullundefined hide the file input button
no-dropbooleanfalse Disable drag and drop mode
no-traversebooleanfalse Wether to returns files as a flat array when in `directory` mode
plainbooleanfalse Don't add any additional styling or classes to the file input
requiredbooleanundefined Adds the `required` attribute to the form control
sizeSize'md' Set the size of the component's appearance. 'sm', 'md' (default), or 'lg'
stateboolean | nullundefined Controls the validation state appearance of the component. `true` for valid, `false` for invalid, or `null` for no validation state
EventArgsDescription
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)
NameScopeDescription
label