Upload 上传
通过点击或者拖拽上传文件。
ts
import { createApp } from 'vue'
import { Upload as ElUpload } from '@szhn/dh-design-pc'
const app = createApp()
app.use(ElUpload)基础用法
<template>
<el-upload
v-model:file-list="fileList"
class="upload-demo"
action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
multiple
:on-preview="handlePreview"
:on-remove="handleRemove"
:before-remove="beforeRemove"
:limit="3"
:on-exceed="handleExceed"
>
<el-button type="primary">Click to upload</el-button>
<template #tip>
<div class="el-upload__tip">
jpg/png files with a size less than 500KB.
</div>
</template>
</el-upload>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { Message as ElMessage, MessageBox as ElMessageBox } from '@szhn/dh-design-pc'
import type { UploadProps, UploadUserFile } from '@szhn/dh-design-pc'
import { Button as ElButton, Upload as ElUpload } from '@szhn/dh-design-pc'
const fileList = ref<UploadUserFile[]>([
{
name: 'element-plus-logo.svg',
url: 'https://element-plus.org/images/element-plus-logo.svg',
},
{
name: 'element-plus-logo2.svg',
url: 'https://element-plus.org/images/element-plus-logo.svg',
},
])
const handleRemove: UploadProps['onRemove'] = (file, uploadFiles) => {
console.log(file, uploadFiles)
}
const handlePreview: UploadProps['onPreview'] = (uploadFile) => {
console.log(uploadFile)
}
const handleExceed: UploadProps['onExceed'] = (files, uploadFiles) => {
ElMessage.warning(
`The limit is 3, you selected ${files.length} files this time, add up to ${
files.length + uploadFiles.length
} totally`
)
}
const beforeRemove: UploadProps['beforeRemove'] = (uploadFile, uploadFiles) => {
return ElMessageBox.confirm(
`Cancel the transfer of ${uploadFile.name} ?`
).then(
() => true,
() => false
)
}
</script>覆盖前一个文件
<template>
<el-upload
ref="upload"
class="upload-demo"
action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
:limit="1"
:on-exceed="handleExceed"
:auto-upload="false"
>
<template #trigger>
<el-button type="primary">select file</el-button>
</template>
<el-button class="ml-3" type="success" @click="submitUpload">
upload to server
</el-button>
<template #tip>
<div class="el-upload__tip text-red">
limit 1 file, new file will cover the old file
</div>
</template>
</el-upload>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { genFileId } from '@szhn/dh-design-pc'
import type { UploadInstance, UploadProps, UploadRawFile } from '@szhn/dh-design-pc'
import { Button as ElButton, Upload as ElUpload } from '@szhn/dh-design-pc'
const upload = ref<UploadInstance>()
const handleExceed: UploadProps['onExceed'] = (files) => {
upload.value!.clearFiles()
const file = files[0] as UploadRawFile
file.uid = genFileId()
upload.value!.handleStart(file)
}
const submitUpload = () => {
upload.value!.submit()
}
</script>用户头像
在 before-upload 钩子中限制用户上传文件的格式和大小。
<template>
<el-upload
class="avatar-uploader"
action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
:show-file-list="false"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload"
>
<img v-if="imageUrl" :src="imageUrl" class="avatar" alt="上传头像预览" />
<el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
</el-upload>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { Message as ElMessage } from '@szhn/dh-design-pc'
import { Plus } from '@szhn/dh-design-pc/icons'
import type { UploadProps } from '@szhn/dh-design-pc'
import { Icon as ElIcon, Upload as ElUpload } from '@szhn/dh-design-pc'
const imageUrl = ref('')
const handleAvatarSuccess: UploadProps['onSuccess'] = (
response,
uploadFile
) => {
imageUrl.value = URL.createObjectURL(uploadFile.raw!)
}
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
if (rawFile.type !== 'image/jpeg') {
ElMessage.error('Avatar picture must be JPG format!')
return false
} else if (rawFile.size / 1024 / 1024 > 2) {
ElMessage.error('Avatar picture size can not exceed 2MB!')
return false
}
return true
}
</script>
<style scoped>
.avatar-uploader .avatar {
width: 178px;
height: 178px;
display: block;
}
</style>
<style>
.avatar-uploader .el-upload {
border: 1px dashed var(--el-border-color);
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
transition: var(--el-transition-duration-fast);
}
.avatar-uploader .el-upload:hover {
border-color: var(--el-color-primary);
}
.el-icon.avatar-uploader-icon {
font-size: 28px;
color: #8c939d;
width: 178px;
height: 178px;
text-align: center;
}
</style>照片墙
使用 list-type 属性来设定文件列表的样式。
<template>
<el-upload
v-model:file-list="fileList"
action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
list-type="picture-card"
:on-preview="handlePictureCardPreview"
:on-remove="handleRemove"
>
<el-icon><Plus /></el-icon>
</el-upload>
<el-dialog v-model="dialogVisible">
<img w-full :src="dialogImageUrl" alt="Preview Image" />
</el-dialog>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { Plus } from '@szhn/dh-design-pc/icons'
import type { UploadProps, UploadUserFile } from '@szhn/dh-design-pc'
import { Dialog as ElDialog, Icon as ElIcon, Upload as ElUpload } from '@szhn/dh-design-pc'
const fileList = ref<UploadUserFile[]>([
{
name: 'food.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
},
{
name: 'plant-1.png',
url: 'https://fuss10.elemecdn.com/a/3f/3302e58f9a181d2509f3dc0fa68b0jpeg.jpeg',
},
{
name: 'food.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
},
{
name: 'plant-2.png',
url: 'https://fuss10.elemecdn.com/1/34/19aa98b1fcb2781c4fba33d850549jpeg.jpeg',
},
{
name: 'food.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
},
{
name: 'figure-1.png',
url: 'https://fuss10.elemecdn.com/0/6f/e35ff375812431e154d0b3a5a3cb6jpeg.jpeg',
},
{
name: 'food.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
},
{
name: 'figure-2.png',
url: 'https://fuss10.elemecdn.com/9/bb/e27858e973f5d7d3904835f46abbdjpeg.jpeg',
},
])
const dialogImageUrl = ref('')
const dialogVisible = ref(false)
const handleRemove: UploadProps['onRemove'] = (uploadFile, uploadFiles) => {
console.log(uploadFile, uploadFiles)
}
const handlePictureCardPreview: UploadProps['onPreview'] = (uploadFile) => {
dialogImageUrl.value = uploadFile.url!
dialogVisible.value = true
}
</script>自定义缩略图
使用 scoped-slot 属性来改变默认的缩略图模板样式。
<template>
<el-upload action="#" list-type="picture-card" :auto-upload="false">
<el-icon><Plus /></el-icon>
<template #file="{ file }">
<div>
<img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
<span class="el-upload-list__item-actions">
<span
class="el-upload-list__item-preview"
@click="handlePictureCardPreview(file)"
>
<el-icon><zoom-in /></el-icon>
</span>
<span
v-if="!disabled"
class="el-upload-list__item-delete"
@click="handleDownload(file)"
>
<el-icon><Download /></el-icon>
</span>
<span
v-if="!disabled"
class="el-upload-list__item-delete"
@click="handleRemove(file)"
>
<el-icon><Delete /></el-icon>
</span>
</span>
</div>
</template>
</el-upload>
<el-dialog v-model="dialogVisible">
<img w-full :src="dialogImageUrl" alt="Preview Image" />
</el-dialog>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { Delete, Download, Plus, ZoomIn } from '@szhn/dh-design-pc/icons'
import type { UploadFile } from '@szhn/dh-design-pc'
import { Dialog as ElDialog, Icon as ElIcon, Upload as ElUpload } from '@szhn/dh-design-pc'
const dialogImageUrl = ref('')
const dialogVisible = ref(false)
const disabled = ref(false)
const handleRemove = (file: UploadFile) => {
console.log(file)
}
const handlePictureCardPreview = (file: UploadFile) => {
dialogImageUrl.value = file.url!
dialogVisible.value = true
}
const handleDownload = (file: UploadFile) => {
console.log(file)
}
</script>图片列表缩略图
<template>
<el-upload
v-model:file-list="fileList"
class="upload-demo"
action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
:on-preview="handlePreview"
:on-remove="handleRemove"
list-type="picture"
>
<el-button type="primary">Click to upload</el-button>
<template #tip>
<div class="el-upload__tip">
jpg/png files with a size less than 500kb
</div>
</template>
</el-upload>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import type { UploadProps, UploadUserFile } from '@szhn/dh-design-pc'
import { Button as ElButton, Upload as ElUpload } from '@szhn/dh-design-pc'
const fileList = ref<UploadUserFile[]>([
{
name: 'food.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
},
{
name: 'food2.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
},
])
const handleRemove: UploadProps['onRemove'] = (uploadFile, uploadFiles) => {
console.log(uploadFile, uploadFiles)
}
const handlePreview: UploadProps['onPreview'] = (file) => {
console.log(file)
}
</script>上传文件列表控制
通过 on-change 钩子函数来对上传文件的列表进行控制。
<template>
<el-upload
v-model:file-list="fileList"
class="upload-demo"
action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
:on-change="handleChange"
>
<el-button type="primary">Click to upload</el-button>
<template #tip>
<div class="el-upload__tip">
jpg/png files with a size less than 500kb
</div>
</template>
</el-upload>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import type { UploadProps, UploadUserFile } from '@szhn/dh-design-pc'
import { Button as ElButton, Upload as ElUpload } from '@szhn/dh-design-pc'
const fileList = ref<UploadUserFile[]>([
{
name: 'food.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
},
{
name: 'food2.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
},
])
const handleChange: UploadProps['onChange'] = (uploadFile, uploadFiles) => {
fileList.value = fileList.value.slice(-3)
}
</script>拖拽上传
你可以将文件拖拽到特定区域以进行上传。
<template>
<el-upload
class="upload-demo"
drag
action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
multiple
>
<el-icon class="el-icon--upload"><upload-filled /></el-icon>
<div class="el-upload__text">
Drop file here or <em>click to upload</em>
</div>
<template #tip>
<div class="el-upload__tip">
jpg/png files with a size less than 500kb
</div>
</template>
</el-upload>
</template>
<script setup lang="ts">
import { UploadFilled } from '@szhn/dh-design-pc/icons'
import { Icon as ElIcon, Upload as ElUpload } from '@szhn/dh-design-pc'
</script>上传目录 (2.13.1)
通过 directory 属性启用文件夹上传。
<template>
<el-upload
class="upload-demo"
drag
action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
directory
multiple
:on-change="handleChange"
>
<el-icon class="el-icon--upload"><upload-filled /></el-icon>
<div class="el-upload__text">
Drop directory here or <em>click to upload</em>
</div>
</el-upload>
</template>
<script setup lang="ts">
import { UploadFilled } from '@szhn/dh-design-pc/icons'
import type { UploadFile, UploadFiles } from '@szhn/dh-design-pc'
import { Icon as ElIcon, Upload as ElUpload } from '@szhn/dh-design-pc'
const handleChange = (uploadFile: UploadFile, uploadFiles: UploadFiles) => {
console.log(uploadFile, uploadFiles)
}
</script>手动上传
<template>
<el-upload
ref="uploadRef"
class="upload-demo"
action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
:auto-upload="false"
>
<template #trigger>
<el-button type="primary">select file</el-button>
</template>
<el-button class="ml-3" type="success" @click="submitUpload">
upload to server
</el-button>
<template #tip>
<div class="el-upload__tip">
jpg/png files with a size less than 500kb
</div>
</template>
</el-upload>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import type { UploadInstance } from '@szhn/dh-design-pc'
import { Button as ElButton, Upload as ElUpload } from '@szhn/dh-design-pc'
const uploadRef = ref<UploadInstance>()
const submitUpload = () => {
uploadRef.value!.submit()
}
</script>API
属性
| 名称 | 描述 | 类型 | 默认值 |
|---|---|---|---|
| action required | 请求 URL | string | # |
| headers | 设置上传的请求头部 | object | — |
| method | 设置上传请求方法 | string | post |
| multiple | 是否支持多选文件 | boolean | false |
| data | 上传时附带的额外参数 从 v2.3.13 支持 Awaitable 数据,和 Function | object / Function | {} |
| name | 上传的文件字段名 | string | file |
| with-credentials | 支持发送 cookie 凭证信息 | boolean | false |
| show-file-list | 是否显示已上传文件列表 | boolean | true |
| drag | 是否启用拖拽上传 | boolean | false |
| accept | 接受上传的文件类型(thumbnail-mode 模式下此参数无效) | string | '' |
| crossorigin | 原生属性 crossorigin | enum | — |
| 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 且被 reject,则停止上传。 | Function | — |
| before-remove | 删除文件之前的钩子,参数为上传的文件和文件列表, 若返回 false 或者返回 Promise 且被 reject,则停止删除。 | Function | — |
| file-list / v-model:file-list | 默认上传文件 | array | [] |
| list-type | 文件列表的类型 | enum | text |
| auto-upload | 是否自动上传文件 | boolean | true |
| http-request | 覆盖默认的 Xhr 行为,允许自行实现上传文件的请求 | Function | 请参考ajaxUpload |
| disabled | 是否禁用上传 | boolean | false |
| limit | 允许上传文件的最大数量 | number | — |
| directory 2.13.1 | 是否支持上传文件夹。 启用后,只能选择文件夹;选择文件夹后,文件夹内的文件将被扁平化处理。 | boolean | false |
插槽
| 名称 | 描述 | 类型 |
|---|---|---|
| default | 自定义默认内容 | - |
| trigger | 触发文件选择框的内容 | - |
| tip | 提示说明文字 | - |
| file | 缩略图模板的内容 | object |
外部方法
| 名称 | 描述 | 类型 |
|---|---|---|
| abort | 取消上传请求。 当指定了文件时,中止相应的待上传文件;当未指定文件时,中止所有待上传文件。 | Function |
| submit | 手动上传文件列表 | Function |
| clearFiles | 清空已上传的文件列表(该方法不支持在 before-upload 中调用) | Function |
| handleStart | 手动选择文件 | Function |
| handleRemove | 手动移除文件。 file 和rawFile 已被合并。 rawFile 将在 v2.2.0 中移除 | Function |
