Upload 上传

通过点击或拖拽上传文件。

基础用法

使用 `slot` 来定义上传按钮的类型和文字。使用 `limit` 和 `on-exceed` 属性设定文件上传数量和超出限制时的钩子函数。此外,你可以在 `before-remove` 钩子中阻止文件被删除。

只能上传 jpg/png 文件,且不超过 500KB

覆盖前一个文件

设置 `limit` 和 `on-exceed` 后,在选择新文件时会自动替换旧文件。

限制上传 1 个文件,新文件会覆盖旧文件

    用户头像

    使用 `before-upload` 钩子限制上传文件的格式和大小。

    照片墙

    使用 `list-type` 属性改变文件列表的样式。

    • 按 delete 键可删除
    • 按 delete 键可删除
    • 按 delete 键可删除
    • 按 delete 键可删除
    • 按 delete 键可删除
    • 按 delete 键可删除
    • 按 delete 键可删除
    • 按 delete 键可删除

    自定义缩略图

    使用 `scoped-slot` 来改变默认的缩略图模板。

    带缩略图的文件列表

    只能上传 jpg/png 文件,且不超过 500kb

    文件列表操作

    使用 `on-change` 钩子函数来控制文件上传列表。

    只能上传 jpg/png 文件,且不超过 500kb

    拖拽上传

    您可以将文件拖到某个区域进行上传。

    将文件拖到此处,或点击上传
    只能上传 jpg/png 文件,且不超过 500kb

      手动上传

      只能上传 jpg/png 文件,且不超过 500kb

        API

        属性

        名称描述类型默认值
        action 必填请求 URL。string#
        headers请求头。object
        method设置上传请求方法。stringpost
        multiple是否允许上传多个文件。booleanfalse
        data请求的其他选项。自 v2.3.13 起支持 `Awaitable` 数据和 `Function`。object / Function{}
        名称上传文件的键名。stringfile
        with-credentials是否发送 cookie。booleanfalse
        show-file-list是否显示已上传文件列表。booleantrue
        drag是否激活拖放模式。booleanfalse
        accept可接受的文件类型,当 `thumbnail-mode === true` 时无效。string''
        crossorigin原生属性 crossorigin枚举
        on-preview点击已上传文件时的钩子函数。Function
        on-remove文件被移除时的钩子函数。Function
        on-success上传成功时的钩子函数。Function
        on-error发生某些错误时的钩子函数。Function
        on-progress发生某些进度时的钩子函数。Function
        on-change选择文件、上传成功或上传失败时的钩子函数。Function
        on-exceed超出限制时的钩子函数。Function
        before-upload上传前处理的钩子函数,参数为待上传的文件。如果返回 `false` 或返回一个被拒绝的 `Promise`,则上传将被中止。Function
        before-remove移除文件前的钩子函数,参数为文件和文件列表。如果返回 `false` 或返回一个被拒绝的 `Promise`,则移除将被中止。Function
        file-list / v-model:file-list默认已上传的文件。object[]
        list-type文件列表的类型。枚举文本
        auto-upload是否自动上传文件。booleantrue
        http-request覆盖默认的 xhr 行为,允许您实现自己的上传文件请求。FunctionajaxUpload
        disabled是否禁用上传。booleanfalse
        limit允许上传的最大数量。number

        插槽

        名称描述类型
        default自定义默认内容。-
        trigger触发文件对话框的内容。-
        tip提示内容。-
        file缩略图模板的内容。object

        暴露

        名称描述类型
        abort取消上传请求。Function
        submit手动上传文件列表。Function
        clearFiles清除文件列表(此方法在 `before-upload` 钩子中不受支持)。Function
        handleStart手动选择文件。Function
        handleRemove手动移除文件。`file` 和 `rawFile` 已合并。`rawFile` 将在 `v2.2.0` 中移除。Function

        类型声明

        显示声明
        ts
        type UploadFiles = UploadFile[]
        
        type UploadUserFile = Omit<UploadFile, 'status' | 'uid'> &
          Partial<Pick<UploadFile, 'status' | 'uid'>>
        
        type UploadStatus = 'ready' | 'uploading' | 'success' | 'fail'
        
        type Awaitable<T> = Promise<T> | T
        
        type Mutable<T> = { -readonly [P in keyof T]: T[P] }
        
        interface UploadFile {
          name: string
          percentage?: number
          status: UploadStatus
          size?: number
          response?: unknown
          uid: number
          url?: string
          raw?: UploadRawFile
        }
        
        interface UploadProgressEvent extends ProgressEvent {
          percent: number
        }
        
        interface UploadRawFile extends File {
          uid: number
          isDirectory?: boolean
        }
        
        interface UploadRequestOptions {
          action: string
          method: string
          data: Record<string, string | Blob | [string | Blob, string]>
          filename: string
          file: File
          headers: Headers | Record<string, string | number | null | undefined>
          onError: (evt: UploadAjaxError) => void
          onProgress: (evt: UploadProgressEvent) => void
          onSuccess: (response: any) => void
          withCredentials: boolean
        }

        来源

        组件样式文档

        贡献者