跳到主要內容

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://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.dev.org.tw/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 支援,優化 bundle 並清理 (#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 許可證下發布。