跳到主要内容

useFileDialog

分类
导出大小
702 B
上次更改
3 个月前

轻松打开文件对话框。

演示

用法

vue
<script setup lang="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 */ }) </script> <template> <
button
type
="button" @
click
="
open
">
Choose file </
button
>
</template>

类型声明

显示类型声明
ts
export interface UseFileDialogOptions extends ConfigurableDocument {
  /**
   * @default true
   */
  
multiple
?:
MaybeRef
<boolean>
/** * @default '*' */
accept
?:
MaybeRef
<string>
/** * Select the input source for the capture file. * @see [HTMLInputElement Capture](https://mdn.org.cn/en-US/docs/Web/HTML/Attributes/capture) */
capture
?:
MaybeRef
<string>
/** * Reset when open file dialog. * @default false */
reset
?:
MaybeRef
<boolean>
/** * Select directories instead of files. * @see [HTMLInputElement webkitdirectory](https://mdn.org.cn/en-US/docs/Web/API/HTMLInputElement/webkitdirectory) * @default false */
directory
?:
MaybeRef
<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
} /** * Open file dialog with ease. * * @see https://vueuse.org.cn/useFileDialog * @param options */ export declare function
useFileDialog
(
options
?: UseFileDialogOptions,
): UseFileDialogReturn

来源

源文件演示文档

贡献者

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

更新日志

v13.6.0
a44bd - 特性: 为 multiple, accept, capture, reset 和 directory 添加 MaybeRef (#4813)
v13.2.0
0ea16 - 特性: 允许文件对话框自定义输入元素 (#4679)
v12.5.0
8a8d6 - 特性: 添加 initialFiles 选项 (#4509)
v12.3.0
59f75 - feat(toValue): 废弃 @vueuse/shared 中的 toValue,转而使用 Vue 的原生函数
0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)
v11.1.0
30452 - 特性: 返回 onCancel 处理程序 (#4184)
424a2 - 修复: 在触发 onChange 之前检查输入值是否存在 (#3972)
v10.7.0
7e2da - 特性: 重置时触发 onChange (#3548)
v10.6.0
cefca - 特性: 添加 directory 参数 (#3513)
v10.2.0
6d847 - 特性: 添加参数 reset (#3059)

根据 MIT 许可证发布。