文件上传增加拖拽

This commit is contained in:
不做码农 2022-03-10 15:02:59 +08:00
parent 6a3547dad2
commit 321619763e

View File

@ -1,9 +1,12 @@
<template> <template>
<div class="upload-file"> <div class="upload-file">
<el-upload :action="uploadFileUrl" :before-upload="handleBeforeUpload" :file-list="fileList" :limit="limit" :on-error="handleUploadError" <el-upload :action="uploadFileUrl" :before-upload="handleBeforeUpload" :file-list="fileList" :limit="limit" :on-error="handleUploadError"
:on-exceed="handleExceed" :on-success="handleUploadSuccess" :on-progress="uploadProcess" :show-file-list="false" :data="data" :headers="headers" ref="upload"> :on-exceed="handleExceed" :on-success="handleUploadSuccess" :on-progress="uploadProcess" :show-file-list="true" :data="data" :headers="headers"
:drag="drag" ref="upload">
<i class="el-icon-upload" v-if="drag"></i>
<div class="el-upload__text" v-if="drag">将文件拖到此处<em>点击上传</em></div>
<!-- 上传按钮 --> <!-- 上传按钮 -->
<el-button size="mini" type="primary" icon="el-icon-upload">选取文件</el-button> <el-button size="mini" icon="el-icon-upload" v-if="!drag">选取文件</el-button>
<!-- 上传提示 --> <!-- 上传提示 -->
<div class="el-upload__tip" slot="tip" v-if="showTip"> <div class="el-upload__tip" slot="tip" v-if="showTip">
请上传 请上传
@ -28,81 +31,86 @@
</template> </template>
<script> <script>
import { getToken } from "@/utils/auth"; import { getToken } from '@/utils/auth'
export default { export default {
name: "FileUpload", name: 'FileUpload',
props: { props: {
// //
value: [String, Object, Array], value: [String, Object, Array],
// //
limit: { limit: {
type: Number, type: Number,
default: 1, default: 1
}, },
// (MB) // (MB)
fileSize: { fileSize: {
type: Number, type: Number,
default: 5, default: 5
}, },
// , ['png', 'jpg', 'jpeg'] // , ['png', 'jpg', 'jpeg']
fileType: { fileType: {
type: Array, type: Array,
default: () => ["doc", "xls", "ppt", "txt", "pdf", "svga", "json"], default: () => ['doc', 'xls', 'ppt', 'txt', 'pdf', 'svga', 'json']
}, },
// //
isShowTip: { isShowTip: {
type: Boolean, type: Boolean,
default: true, default: true
}, },
// //
uploadUrl: { uploadUrl: {
type: String, type: String,
default: process.env.VUE_APP_UPLOAD_URL, default: process.env.VUE_APP_UPLOAD_URL
}, },
// form // form
column: [String], column: [String],
// //
data: { data: {
type: Object, type: Object
}, },
//
drag: {
type: Boolean,
default: false
}
}, },
data() { data() {
return { return {
baseUrl: process.env.VUE_APP_BASE_API, baseUrl: process.env.VUE_APP_BASE_API,
uploadFileUrl: process.env.VUE_APP_BASE_API + this.uploadUrl, // uploadFileUrl: process.env.VUE_APP_BASE_API + this.uploadUrl, //
headers: { headers: {
Authorization: "Bearer " + getToken(), Authorization: 'Bearer ' + getToken()
}, },
fileList: [], fileList: []
}; }
}, },
watch: { watch: {
value: { value: {
handler(val) { handler(val) {
if (val) { if (val) {
let temp = 1; let temp = 1
// //
const list = Array.isArray(val) ? val : this.value.split(","); const list = Array.isArray(val) ? val : this.value.split(',')
// //
this.fileList = list.map((item) => { this.fileList = list.map((item) => {
if (typeof item === "string") { if (typeof item === 'string') {
item = { name: item, url: item }; item = { name: item, url: item }
} }
item.uid = item.uid || new Date().getTime() + temp++; item.uid = item.uid || new Date().getTime() + temp++
return item; return item
}); })
} else { } else {
this.fileList = []; this.fileList = []
return []; return []
} }
}, },
deep: true, deep: true,
immediate: true, immediate: true
}, },
uploadUrl: { uploadUrl: {
handler(val) { handler(val) {
this.uploadFileUrl = process.env.VUE_APP_BASE_API + val; this.uploadFileUrl = process.env.VUE_APP_BASE_API + val
}, },
immediate: true immediate: true
} }
@ -110,59 +118,68 @@ export default {
computed: { computed: {
// //
showTip() { showTip() {
return this.isShowTip && (this.fileType || this.fileSize); return this.isShowTip && (this.fileType || this.fileSize)
}, }
}, },
methods: { methods: {
// //
handleBeforeUpload(file) { handleBeforeUpload(file) {
// //
if (this.fileType && this.fileType.length > 0) { if (this.fileType && this.fileType.length > 0) {
let fileExtension = ""; let fileExtension = ''
if (file.name.lastIndexOf(".") > -1) { if (file.name.lastIndexOf('.') > -1) {
fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1); fileExtension = file.name.slice(file.name.lastIndexOf('.') + 1)
} }
const isTypeOk = this.fileType.some((type) => { const isTypeOk = this.fileType.some((type) => {
if (file.type.indexOf(type) > -1) return true; if (file.type.indexOf(type) > -1) return true
if (fileExtension && fileExtension.indexOf(type) > -1) return true; if (fileExtension && fileExtension.indexOf(type) > -1) return true
return false; return false
}); })
if (!isTypeOk) { if (!isTypeOk) {
this.msgError( this.msgError(
`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!` `文件格式不正确, 请上传${this.fileType.join('/')}格式文件!`
); )
return false; return false
} }
} }
// //
if (this.fileSize) { if (this.fileSize) {
const isLt = file.size / 1024 / 1024 < this.fileSize; const isLt = file.size / 1024 / 1024 < this.fileSize
if (!isLt) { if (!isLt) {
this.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`); this.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`)
return false; return false
} }
} }
return true; return true
}, },
// //
handleExceed() { handleExceed() {
this.msgError(`上传文件数量不能超过 ${this.limit} 个!`); this.msgError(`上传文件数量不能超过 ${this.limit} 个!`)
}, },
// //
handleUploadError(err) { handleUploadError(err) {
this.msgError("上传失败, 请重试"); this.msgError('上传失败, 请重试')
this.fileList = []; this.fileList = []
}, },
// //
handleUploadSuccess(res, file) { handleUploadSuccess(res, file) {
if (res.code != 200) { if (res.code != 200) {
this.fileList = []; this.fileList = []
this.msgError(`上传失败,原因:${res.msg}!`); this.msgError(`上传失败,原因:${res.msg}!`)
return; return
} }
this.msgSuccess("上传成功"); this.msgSuccess('上传成功')
this.fileList.push({ name: res.data.fileName, url: res.data.url, path: res.data.path }); this.fileList.push({
this.$emit("input", this.column, this.listToString(this.fileList), res.data); name: res.data.fileName,
url: res.data.url,
path: res.data.path
})
this.$emit(
'input',
this.column,
this.listToString(this.fileList),
res.data
)
}, },
// //
uploadProcess(event, file, fileList) { uploadProcess(event, file, fileList) {
@ -170,28 +187,28 @@ export default {
}, },
// //
handleDelete(index) { handleDelete(index) {
this.fileList.splice(index, 1); this.fileList.splice(index, 1)
this.$emit("input", this.column, this.listToString(this.fileList)); this.$emit('input', this.column, this.listToString(this.fileList))
}, },
// //
getFileName(name) { getFileName(name) {
if (name.lastIndexOf("/") > -1) { if (name.lastIndexOf('/') > -1) {
return name.slice(name.lastIndexOf("/") + 1).toLowerCase(); return name.slice(name.lastIndexOf('/') + 1).toLowerCase()
} else { } else {
return name; return name
} }
}, },
// //
listToString(list, separator) { listToString(list, separator) {
let strs = ""; let strs = ''
separator = separator || ","; separator = separator || ','
for (let i in list) { for (const i in list) {
strs += list[i].url + separator; strs += list[i].url + separator
}
return strs != '' ? strs.substr(0, strs.length - 1) : ''
}
}
} }
return strs != "" ? strs.substr(0, strs.length - 1) : "";
},
},
};
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
@ -210,4 +227,8 @@ export default {
.ele-upload-list__item-content-action .el-link { .ele-upload-list__item-content-action .el-link {
margin-right: 10px; margin-right: 10px;
} }
::v-deep .el-upload-dragger {
width: 220px;
height: 150px;
}
</style> </style>