帮我写一段基于vue-upload-component 和ElementUi 封装 uploader-file标签的代码
时间: 2023-03-27 18:03:22 浏览: 159
好的,以下是基于 vue-upload-component 和 ElementUi 封装 uploader-file 标签的代码:
<template>
<div>
<el-upload
class="upload-demo"
:action="uploadUrl"
:on-success="handleSuccess"
:before-upload="beforeUpload"
:file-list="fileList"
:auto-upload="false"
:show-file-list="false"
>
<el-button slot="trigger" size="small" type="primary">选取文件</el-button>
<el-button size="small" type="success" @click="submitUpload">上传到服务器</el-button>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>
</div>
</template>
<script>
import { Upload } from 'vue-upload-component'
import { Message } from 'element-ui'
export default {
components: {
Upload
},
data() {
return {
uploadUrl: 'your-upload-url',
fileList: []
}
},
methods: {
handleSuccess(response, file, fileList) {
Message.success('上传成功')
console.log(response, file, fileList)
},
beforeUpload(file) {
const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
const isLt500K = file.size / 1024 < 500
if (!isJPG) {
Message.error('上传图片只能是 JPG/PNG 格式!')
}
if (!isLt500K) {
Message.error('上传图片大小不能超过 500KB!')
}
return isJPG && isLt500K
},
submitUpload() {
this.$refs.upload.submit()
}
}
}
</script>
阅读全文