跳到内容

useFileDialog

类别
导出大小
577 B
最后更改
3 周前

轻松打开文件对话框。

演示

用法

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

const { files, open, reset, onCancel, onChange } = useFileDialog({
  accept: 'image/*', // Set to accept only image files
  directory: true, // Select directories instead of files if set true
})

onChange((files) => {
  /** do something with files */
})

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

类型声明

显示类型声明
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://mdn.org.cn/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://mdn.org.cn/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.cn/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 于 2025/1/22
8a8d6 - feat: 添加 initialFiles 选项 (#4509)
v12.3.0 于 2025/1/2
59f75 - feat(toValue): 弃用 @vueuse/shared 中的 toValue,转而使用 Vue 原生方法
v12.0.0-beta.1 于 2024/11/21
0a9ed - feat!: 移除 Vue 2 支持,优化包并清理 (#4349)
v11.1.0 于 2024/9/16
30452 - feat: 返回 onCancel 处理程序 (#4184)
v10.10.0 于 2024/5/27
424a2 - fix: 检查输入值是否存在,然后再触发 onChange (#3972)
v10.7.0 于 2023/12/5
7e2da - feat: 重置时触发 onChange (#3548)
v10.6.0 于 2023/11/9
cefca - feat: 添加目录参数 (#3513)
v10.2.0 于 2023/6/16
6d847 - feat: 添加参数 reset (#3059)
v10.0.0-beta.1 于 2023/3/23
5e697 - feat: 为文件更改事件添加监听器 (#2893)

在 MIT 许可证下发布。