Skip to content

useFileDialog

类别
导出体积
577 B
上次更改
3 weeks 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
}
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
Yauheni Vasiukevich
Andrey Yolkin
jinkaiqin
Doctorwu
huiliangShen
Sandra Rodgers
Damian Głowala
zaqvil
丶远方
1SZX1
ZHAO Jinxiang
Max
Robert Soriano

更新日志

v12.5.0 on 1/22/2025
8a8d6 - feat: add initialFiles option (#4509)
v12.3.0 on 1/2/2025
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.0.0-beta.1 on 11/21/2024
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v11.1.0 on 9/16/2024
30452 - feat: return onCancel handler (#4184)
v10.10.0 on 5/27/2024
424a2 - fix: check if input value exists before trigger onChange (#3972)
v10.7.0 on 12/5/2023
7e2da - feat: trigger onChange when reset (#3548)
v10.6.0 on 11/9/2023
cefca - feat: add directory parameters (#3513)
v10.2.0 on 6/16/2023
6d847 - feat: add a parameter reset (#3059)
v10.0.0-beta.1 on 3/23/2023
5e697 - feat: add listener for file change events (#2893)