Skip to content

useFileDialog

Category
Export Size
577 B
Last Changed
2 months ago

轻松打开文件对话框。

Demo

使用方法

ts
import { useFileDialog } from '@vueuse/core'

const { files, open, reset, onCancel, onChange } = useFileDialog({
  accept: 'image/*', // 设置仅接受图像文件
  directory: true, // 如果设置为 true,则选择目录而不是文件
})

onChange((files) => {
  /** 处理文件 */
})

onCancel(() => {
  /** do something on cancel */
})
vue
<template>
  <button type="button" @click="open">
    Choose file
  </button>
</template>

Type Declarations

Show Type Declarations
typescript
export interface UseFileDialogOptions extends ConfigurableDocument {
  /**
   * @default true
   */
  multiple?: boolean
  /**
   * @default '*'
   */
  accept?: string
  /**
   * Select the input source for the capture file.
   * @see [HTMLInputElement Capture](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/capture)
   */
  capture?: string
  /**
   * Reset when open file dialog.
   * @default false
   */
  reset?: boolean
  /**
   * Select directories instead of files.
   * @see [HTMLInputElement webkitdirectory](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/webkitdirectory)
   * @default false
   */
  directory?: boolean
  /**
   * Initial files to set.
   * @default null
   */
  initialFiles?: Array<File> | FileList
}
export interface UseFileDialogReturn {
  files: Ref<FileList | null>
  open: (localOptions?: Partial<UseFileDialogOptions>) => void
  reset: () => void
  onChange: EventHookOn<FileList | null>
  onCancel: EventHookOn
}
/**
 * Open file dialog with ease.
 *
 * @see https://vueuse.org/useFileDialog
 * @param options
 */
export declare function useFileDialog(
  options?: UseFileDialogOptions,
): UseFileDialogReturn

Source

SourceDemoDocs

Contributors

丶远方
Anthony Fu
Anthony Fu
Yauheni Vasiukevich
Andrey Yolkin
jinkaiqin
Doctorwu
huiliangShen
Sandra Rodgers
Damian Głowala
zaqvil
1SZX1
ZHAO Jinxiang
Max
Robert Soriano

Changelog

8a8d6 - feat: add initialFiles option (#4509)
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
30452 - feat: return onCancel handler (#4184)
424a2 - fix: check if input value exists before trigger onChange (#3972)
7e2da - feat: trigger onChange when reset (#3548)
cefca - feat: add directory parameters (#3513)
6d847 - feat: add a parameter reset (#3059)

Released under the MIT License.