<template>
|
<div>
|
<el-upload
|
ref="upload"
|
class="upload-demo"
|
:headers="headers"
|
:action="fileUploadApi + '?name=' + new Date().getTime()"
|
:file-list="fileList"
|
:auto-upload="false"
|
drag
|
multiple
|
:limit="limit"
|
:on-change="handleChange"
|
:on-exceed="handleExceed"
|
:on-remove="handleRemove"
|
:on-success="handleSuccess"
|
:on-error="handleError"
|
:on-preview="handlePreview"
|
:disabled="disabled"
|
>
|
<i class="el-icon-upload" />
|
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
|
<div slot="tip" class="el-upload__tip" />
|
</el-upload>
|
</div>
|
</template>
|
|
<script>
|
import { mapGetters } from 'vuex'
|
import { getToken } from '@/utils/auth'
|
export default {
|
name: 'FileUpload',
|
props: {
|
fileUrl: {
|
type: String,
|
default() {
|
return null
|
}
|
},
|
limit: {
|
type: Number,
|
default() {
|
return 1
|
}
|
},
|
disabled: {
|
type: Boolean,
|
default() {
|
return false
|
}
|
}
|
},
|
data() {
|
return {
|
// 请求头
|
headers: { 'Authorization': getToken() },
|
// 文件列表
|
fileList: []
|
}
|
},
|
computed: {
|
...mapGetters([
|
'fileUploadApi',
|
'baseApi'
|
])
|
},
|
mounted() {
|
// 判断文件路径参数是否为空
|
if (this.fileUrl !== null) {
|
// 文件路径数组
|
const fileUrlList = this.fileUrl.split(',')
|
// 循环文件路径数组
|
for (let i = 0; i < fileUrlList.length; i++) {
|
// 临时集合
|
const tempOption = {}
|
// 临时集合添加文件路径参数
|
tempOption.url = this.baseApi + fileUrlList[i]
|
tempOption.name = fileUrlList[i].substring(fileUrlList[i].lastIndexOf('/') + 1, fileUrlList[i].lastIndexOf('-')) + fileUrlList[i].substring(fileUrlList[i].lastIndexOf('.'), fileUrlList[i].length)
|
// 文件列表添加临时集合
|
this.fileList.push(tempOption)
|
}
|
}
|
},
|
methods: {
|
// 改变事件
|
handleChange(file, fileList) {
|
// 文件列表赋值
|
this.fileList = fileList
|
},
|
// 个数限制事件
|
handleExceed(files, fileList) {
|
this.$message.warning('文件个数超出上限,请先移除文件再上传!')
|
},
|
// 移除事件
|
handleRemove(file, fileList) {
|
// 文件列表赋值
|
this.fileList = fileList
|
},
|
// 成功事件
|
handleSuccess(response, file, fileList) {
|
// 循环文件列表
|
for (let i = 0; i < fileList.length; i++) {
|
// 判断文件状态
|
if (fileList[i].status === 'ready' || fileList[i].status === 'uploading') {
|
// 终止
|
return
|
}
|
// 判断循环是否结束
|
if (i === fileList.length - 1) {
|
// 向父页面发送参数:“fileInfo”是参数名,“fileList”是参数值。
|
this.$emit('fileInfo', fileList)
|
}
|
}
|
},
|
// 失败事件
|
handleError(err) {
|
console.log(err)
|
},
|
// 点击事件
|
handlePreview(file) {
|
var a = document.createElement('a')
|
var event = new MouseEvent('click')
|
a.download = file.name
|
a.href = file.url
|
a.target = '_blank'
|
a.dispatchEvent(event)
|
},
|
// 上传文件
|
submit() {
|
// 判断文件列表长度
|
if (this.fileList.length === 0) {
|
// 向父页面发送参数:“fileInfo”是参数名,“this.fileList”是参数值。
|
this.$emit('fileInfo', this.fileList)
|
// 终止
|
return
|
}
|
// 循环文件列表
|
for (let i = 0; i < this.fileList.length; i++) {
|
// 判断文件状态
|
if (this.fileList[i].status === 'ready') {
|
// 将指定组件别名进行提交
|
this.$refs.upload.submit()
|
// 终止
|
return
|
}
|
// 判断循环是否结束
|
if (i === this.fileList.length - 1) {
|
// 向父页面发送参数:“fileInfo”是参数名,“this.fileList”是参数值。
|
this.$emit('fileInfo', this.fileList)
|
}
|
}
|
}
|
}
|
}
|
</script>
|
|
<style>
|
|
</style>
|