cmc-web/packages/common/components/upload-file/index.vue

108 lines
3.0 KiB
Vue
Raw Normal View History

2024-08-20 12:11:31 +00:00
<template>
<div>
<el-form-item :label="label" required>
2024-08-21 01:17:14 +00:00
<input :id="id" type="file" name="file" v-if="isShowUploadFile" style="border: 1px solid #d8dce5; border-radius: 4px; padding: 5px 10px; width: 80%" />
2024-08-20 12:11:31 +00:00
<el-button type="primary" v-if="isShowUploadFile" @click="submitUpload(file)"></el-button>
<el-progress :percentage="file.progress" v-if="!isShowUploadFile"></el-progress>
</el-form-item>
</div>
</template>
<script>
2024-08-21 01:17:14 +00:00
import uploadFile from 'utils/uploadFile'
2024-08-20 12:11:31 +00:00
export default {
props: {
fileType: {
type: String
},
id: {
type: String,
default: 'btnFileUpload'
},
label: {
type: String,
default: '文件上传:'
}
},
2024-08-21 01:17:14 +00:00
data() {
2024-08-20 12:11:31 +00:00
return {
file: null,
isShowUploadFile: true,
uploadSuccess: false
2024-08-21 01:17:14 +00:00
}
2024-08-20 12:11:31 +00:00
},
2024-08-21 01:17:14 +00:00
created() {},
mounted() {
const self = this
2024-08-20 12:11:31 +00:00
document.getElementById(this.id).addEventListener('change', function (event) {
2024-08-21 01:17:14 +00:00
const files = event.target.files
const file = files[0]
2024-08-20 12:11:31 +00:00
if (/[\u4e00-\u9fa5\s]/.test(file.name)) {
self.$message({
message: '文件名不允许存在中文和空格',
type: 'error'
2024-08-21 01:17:14 +00:00
})
2024-08-20 12:11:31 +00:00
}
if (file.name.length > 64) {
self.$message({
message: '文件名称过长',
type: 'error'
2024-08-21 01:17:14 +00:00
})
2024-08-20 12:11:31 +00:00
}
self.file = {
file: file,
name: file.name,
isUploading: false,
isCancel: false,
isReady: false,
isSuccess: false,
progress: 0
2024-08-21 01:17:14 +00:00
}
})
2024-08-20 12:11:31 +00:00
},
methods: {
2024-08-21 01:17:14 +00:00
submitUpload(file) {
2024-08-20 12:11:31 +00:00
if (file == undefined || file == null) {
2024-08-21 01:17:14 +00:00
return this.$message.error('请选择上传文件')
2024-08-20 12:11:31 +00:00
}
if (/[\u4e00-\u9fa5\s]/.test(file.name)) {
2024-08-21 01:17:14 +00:00
return this.$message.error('文件名不允许存在中文和空格')
2024-08-20 12:11:31 +00:00
}
if (this.fileType == 'EXCEL') {
2024-08-21 01:17:14 +00:00
const fileName = file.name.split('.')
2024-08-20 12:11:31 +00:00
let flag
2024-08-21 01:17:14 +00:00
if (
fileName[fileName.length - 1] === 'xlsx' ||
fileName[fileName.length - 1] === 'xls' ||
fileName[fileName.length - 1] === 'xltx' ||
fileName[fileName.length - 1] === 'xlt' ||
fileName[fileName.length - 1] === 'xlsm' ||
fileName[fileName.length - 1] === 'xlsb' ||
fileName[fileName.length - 1] === 'xltm' ||
fileName[fileName.length - 1] === 'csv'
2024-08-20 12:11:31 +00:00
) {
flag = true
} else {
2024-08-21 01:17:14 +00:00
return this.$message.error('请上传EXCEL表格')
2024-08-20 12:11:31 +00:00
}
} else if (this.fileType == 'TXT') {
const fileName = file.name.split('.')
if (['xlsx', 'xls', 'csv', 'png', 'jpg', 'jpeg', 'gif', 'exe', 'sys', 'mp4', 'avi', 'wmv', 'pdf', 'md'].includes(fileName[fileName.length - 1])) return this.$message.error('请上传文本类文件')
}
2024-08-21 01:17:14 +00:00
this.isShowUploadFile = false
this.$emit('show', this.isShowUploadFile)
uploadFile(file, this.message)
2024-08-20 12:11:31 +00:00
},
2024-08-21 01:17:14 +00:00
message(item) {
2024-08-20 12:11:31 +00:00
this.$message({
message: '上传成功',
type: 'success'
2024-08-21 01:17:14 +00:00
})
this.uploadSuccess = true
2024-08-20 12:11:31 +00:00
}
}
2024-08-21 01:17:14 +00:00
}
2024-08-20 12:11:31 +00:00
</script>