EleFormUploadFile 文件上传组件
组件介绍
EleFormUploadFile 是一个基于 Element UI 封装的文件上传组件,用于在表单中快速实现文件上传功能。它支持单个和多个文件上传、文件预览、文件删除等功能。
安装与引入
安装依赖
npm install diandi-ele-form-upload-file
全局引入
在 src/projects/scrm/utils/el-form.js 中全局引入和配置:
import Vue from 'vue'
import EleFormUploadFile from 'diandi-ele-form-upload-file'
// 注册 upload-file 组件
Vue.component('upload-file', EleFormUploadFile)
// 全局配置
Vue.use(EleForm, {
// 专门设置全局的 upload-file 属性
'upload-file': {
action: store.getters.elForm.uploadFile, // 上传地址
responseFn(response, file, fileList) {
console.log('城南擦肩可能会', fileList)
return {
name: file.name,
attachment: response.data.attachment,
url: response.data.url,
size: file.size
}
}
}
})
全局配置
// 全局 upload-file 配置
'upload-file': {
action: String, // 上传地址
responseFn: Function // 响应处理函数
}
// 上传配置
upload: {
fileSize: Number, // 文件大小限制
chunkSize: Number, // 分片尺寸(M)
uploadFile: String, // 文件上传地址
uploadMerge: String, // 分配合并地址
headers: Object // 请求头
}
基本使用
单个文件上传
<template>
<ele-form
:form-data="formData"
:form-config="formConfig"
@submit="handleSubmit"
/>
</template>
<script>
export default {
data() {
return {
formData: {
resume: ''
},
formConfig: [
{
label: '简历',
prop: 'resume',
type: 'upload-file',
props: {
limit: 1
}
}
]
}
},
methods: {
handleSubmit(data) {
console.log('表单数据:', data)
}
}
}
</script>
多个文件上传
<template>
<ele-form
:form-data="formData"
:form-config="formConfig"
@submit="handleSubmit"
/>
</template>
<script>
export default {
data() {
return {
formData: {
attachments: []
},
formConfig: [
{
label: '附件',
prop: 'attachments',
type: 'upload-file',
props: {
limit: 5,
multiple: true
}
}
]
}
},
methods: {
handleSubmit(data) {
console.log('表单数据:', data)
}
}
}
</script>
属性配置
基础属性
| 属性 | 类型 | 描述 | 默认值 |
|---|---|---|---|
| action | String | 上传地址 | - |
| method | String | 上传方法 | 'post' |
| multiple | Boolean | 是否支持多选 | false |
| limit | Number | 上传数量限制 | - |
| fileSize | Number | 文件大小限制(MB) | - |
| accept | String | 接受的文件类型 | '*' |
| headers | Object | 请求头 | {} |
| data | Object | 上传额外参数 | {} |
| withCredentials | Boolean | 是否携带凭证 | false |
| showFileList | Boolean | 是否显示文件列表 | true |
| drag | Boolean | 是否支持拖拽上传 | false |
| listType | String | 文件列表类型 | 'text' |
| autoUpload | Boolean | 是否自动上传 | true |
| onRemove | Function | 文件移除前的钩子 | - |
| onPreview | Function | 点击文件列表中已上传文件时的钩子 | - |
| onSuccess | Function | 文件上传成功时的钩子 | - |
| onError | Function | 文件上传失败时的钩子 | - |
| onProgress | Function | 文件上传时的钩子 | - |
| onExceed | Function | 文件超出限制时的钩子 | - |
| beforeUpload | Function | 上传文件之前的钩子 | - |
| responseFn | Function | 响应处理函数 | - |
| disabled | Boolean | 是否禁用 | false |
| placeholder | String | 占位符文本 | - |
| tip | String | 提示文本 | - |
| listStyle | Object | 列表样式 | {} |
| uploadStyle | Object | 上传区域样式 | {} |
响应处理函数
responseFn(response, file, fileList) {
console.log('城南擦肩可能会', fileList)
return {
name: file.name,
attachment: response.data.attachment,
url: response.data.url,
size: file.size
}
}
参数说明:
response:服务器返回的响应数据file:当前上传的文件对象fileList:文件列表
返回值:
- 对象:包含以下属性的对象
name:文件名attachment:文件存储路径url:文件预览地址size:文件大小
事件
| 事件名称 | 回调参数 | 描述 |
|---|---|---|
| change | fileList | 文件列表变化时触发 |
| success | response, file, fileList | 文件上传成功时触发 |
| error | error, file, fileList | 文件上传失败时触发 |
| progress | event, file, fileList | 文件上传进度变化时触发 |
| remove | file, fileList | 文件移除时触发 |
| preview | file | 文件预览时触发 |
| exceed | file, fileList | 文件超出限制时触发 |
常见问题
1. 上传失败
原因:上传地址配置错误或服务器端问题
解决方案:
- 检查
action配置是否正确 - 确认服务器端是否正常接收上传请求
- 检查网络连接和跨域配置
2. 响应处理错误
原因:responseFn 配置错误或服务器返回格式不匹配
解决方案:
- 检查
responseFn函数是否正确处理服务器返回的数据 - 确认服务器返回的数据格式是否包含
data.attachment和data.url字段 - 调整
responseFn函数以适应服务器返回的数据格式
3. 文件类型限制
原因:accept 配置错误或文件类型不匹配
解决方案:
- 检查
accept配置是否正确 - 对于特定文件类型,设置相应的 MIME 类型
- 对于多种文件类型,使用逗号分隔
示例代码
完整示例
<template>
<ele-form
:form-data="formData"
:form-config="formConfig"
:label-width="'120px'"
@submit="handleSubmit"
/>
</template>
<script>
export default {
data() {
return {
formData: {
resume: '',
attachments: []
},
formConfig: [
{
label: '简历',
prop: 'resume',
type: 'upload-file',
required: true,
props: {
limit: 1,
accept: '.pdf,.doc,.docx',
tip: '请上传 PDF、Word 格式的简历'
}
},
{
label: '附件',
prop: 'attachments',
type: 'upload-file',
props: {
limit: 5,
multiple: true,
drag: true,
tip: '请上传附件,最多 5 个文件',
fileSize: 50, // 限制 50MB
listType: 'text'
}
}
]
}
},
methods: {
handleSubmit(data) {
console.log('表单数据:', data)
// 这里可以进行API调用等操作
this.$message.success('提交成功')
}
}
}
</script>
<style scoped>
/* 自定义样式 */
</style>
自定义响应处理
<template>
<ele-form
:form-data="formData"
:form-config="formConfig"
@submit="handleSubmit"
/>
</template>
<script>
export default {
data() {
return {
formData: {
files: []
},
formConfig: [
{
label: '文件',
prop: 'files',
type: 'upload-file',
props: {
limit: 3,
multiple: true,
responseFn: (response, file, fileList) => {
// 自定义响应处理
if (response.code === 200) {
return {
name: file.name,
attachment: response.data.path,
url: response.data.url,
size: file.size
}
}
throw new Error(response.message || '上传失败')
}
}
}
]
}
},
methods: {
handleSubmit(data) {
console.log('表单数据:', data)
}
}
}
</script>
总结
EleFormUploadFile 是一个功能强大的文件上传组件,通过简单的配置即可快速实现在表单中的文件上传功能。它支持多种上传模式和配置选项,可以满足不同场景下的文件上传需求。
通过全局配置和组件级配置的结合,可以灵活应对各种文件上传场景,大大提高了开发效率。
版本信息
- 最后更新时间: 2025-03-08
- 维护者: Wang chunsheng 2192138785@qq.com
