Skip to content

useFileDialog

类别
导出体积
577 B
上次更改
3 minutes ago

轻松打开文件对话框。

示例

使用方法

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

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

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

onCancel(() => {
  /** 在取消时做一些事情 */
})
vue
<template>
  <button type="button" @click="open">
    选择文件
  </button>
</template>

类型声明

显示类型声明
typescript
export interface UseFileDialogOptions extends ConfigurableDocument {
  /**
   * @default true
   */
  multiple?: boolean
  /**
   * @default '*'
   */
  accept?: string
  /**
   * 选择捕获文件的输入源。
   * @see [HTMLInputElement Capture](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/capture)
   */
  capture?: string
  /**
   * 打开文件对话框时重置。
   * @default false
   */
  reset?: boolean
  /**
   * 选择目录而不是文件。
   * @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
  /**
   * The input element to use for file dialog.
   * @default document.createElement('input')
   */
  input?: MaybeElementRef<HTMLInputElement>
}
export interface UseFileDialogReturn {
  files: Ref<FileList | null>
  open: (localOptions?: Partial<UseFileDialogOptions>) => void
  reset: () => void
  onChange: EventHookOn<FileList | null>
  onCancel: EventHookOn
}
/**
 * 轻松打开文件对话框。
 *
 * @see https://vueuse.org/useFileDialog
 * @param options
 */
export declare function useFileDialog(
  options?: UseFileDialogOptions,
): UseFileDialogReturn

源码

源码演示文档

贡献者

一纸忘忧
Anthony Fu
Anthony Fu
Ivan Shakhorski
Robin
Yauheni Vasiukevich
Andrey Yolkin
jinkaiqin
Doctorwu
huiliangShen
Sandra Rodgers
Damian Głowala
zaqvil
丶远方
1SZX1
ZHAO Jinxiang
Max
Robert Soriano

更新日志

0ea16 - feat: Allow custom input element for file dialog (#4679)
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)
5e697 - feat: add listener for file change events (#2893)