elementui使用el-upload组件如何实现自定义上传

 更新时间:2022年08月02日 10:43:04   作者:风如也  
这篇文章主要介绍了elementui使用el-upload组件如何实现自定义上传,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

使用el-upload组件实现自定义上传

方式一:选择后自动上传

使用 http-request 覆盖默认的上传行为,可以自定义上传的实现

利用 before-upload 上传文件之前的钩子,参数为上传的文件,若返回 false 或者返回 Promise且被 reject,则停止上传

template 部分

<el-upload
   class="pad"
   ref="upload"
   action="action"
   :http-request="uploadBpmn"
   :before-upload="beforeUpload">
   <el-button size="medium" type="primary" class="el-icon-upload"> 部署流程定义</el-button>
 </el-upload>

js 部分

beforeUpload (file) { // 上传文件之前钩子
  const type = file.name.split('.')[1]
  if (type !== 'bpmn') {
    this.$message({ type: 'error', message: '只支持bpmn文件格式!' })
    return false
  }
},
uploadBpmn (param) { // 部署流程定义(点击按钮,上传bpmn文件,上传成功后部署,然后重新加载列表)
  const formData = new FormData()
  formData.append('processDefinition', param.file) // 传入bpmn文件
  this.$API({
    name: 'deploy',
    data: formData,
    headers: {'Content-Type': 'multipart/form-data'}
  }).then(res => {
    if (res.data.code == 0) {
      this.$message({ type: 'success', message: res.data.msg })
    } else {
      this.$message({ type: 'error', message: res.data.msg })
    }
  }).catch(error => {
    this.$message({ type: 'error', message: error })
  }).finally(() => {
    this.getList()
  })
},

如果不想上传成功后显示上传文件列表,可以隐藏掉文件列表

可以在组件中设置 :show-file-list="false"

或者

::v-deep .el-upload-list {
  display: none !important;
}

方式二:选择后手动上传

<template>
  <div class="app-upload">
    <div class="upload-title">上传文件</div>
    <el-upload
      class="upload-demo"
      ref="uploadBox"
      drag
      action="action"
      :before-upload="beforeUpload"
      :http-request="upload"
      :auto-upload="false"
      multiple>
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
    </el-upload>
    <div class="upload-btn">
      <el-button type="primary" @click="sure" :loading="loading">确 定</el-button>
    </div>
  </div>
</template>
<script>
const formData = new FormData()
export default {
  data () {
    return {
      loading: false
    }
  },
  methods: {
    beforeUpload (file) { // 上传文件之前钩子
      formData.append('files', file)
    },
    upload () {
      this.loading = true
      this.$API({
        name: 'UploadResource',
        data: formData,
        params: {
          path: this.$route.query.path
        },
        requireAuth: true
      }).then (res => {
        if (res.data.code === 200) {
          this.$notify.success(res.data.msg)
        } else {
          this.$notify.error(res.data.msg)
        }
      }).catch(error => {
        this.$notify.error(error)
      }).finally(() => {
        this.loading = false
        this.reset()
      })
    },
    sure () {
      this.$refs.uploadBox.submit()
    },
  }
}
</script>

使用el-upload上传文件夹

只需要为 input 输入框设置 webkitdirectory 属性

  mounted() {
    if (this.$route.query.type === 'folder') {
      this.$nextTick(() => {
        document.querySelector('.el-upload__input').webkitdirectory = true
      })
    }
  },

封装elementui el-upload文件上传组件

// 自定义的全局组件my-component
let _utils = new Utils()
Vue.component(
    'uploadfile', {
        props: {
            uploaddata: {
                type: Object
            }
        },
        template: `<div style="margin:20px 0;">
<el-upload
  accept=".xls,.xlsx"
  class="upload-demo"
  :action="uploaddata.url"
  :before-upload="beforeUpload"
  :on-success="handleSuccess"
  :on-preview="handlePreview"
  :on-remove="handleRemove"
  :before-remove="beforeRemove"
  :limit="2"
  :on-exceed="handleExceed">
  <span>表名称:{{uploaddata.sheetname}}</span>
  <el-button size="small" type="primary">选择文件</el-button>
<!--  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>-->
</el-upload>
</div>`,
        data() {
            return {}
        },
        methods: {
            /*
            上传文件之前的钩子,参数为上传的文件,
            若返回 false 或者返回 Promise 且被 reject,则停止上传。
             */
            beforeUpload(file){
                const fileSuffix = file.name.substring(file.name.lastIndexOf(".")+1);
                const whiteList = ["xls", "xlsx"];
                if (whiteList.indexOf(fileSuffix) === -1) {
                    _utils.MessageError(this,"上传文件只能是xls、xlsx格式")
                    return false;
                }
                const isLt2M = file.size / 1024 / 1024 < 5;
                if (!isLt2M) {
                    _utils.MessageError(this,"上传文件大小不能超过5MB")
                    return false;
                }
            },
            handleRemove(file, fileList) {
             
            },
            handleSuccess(response, file, fileList) {
                let {code, msg} = response
                if (code == 0) {
                    utils.MessageSuccess(this, "文件上传成功")
                } else {
                    utils.MessageError(this, msg)
                }
            },
            /*
            点击文件列表中已上传的文件时的钩子
             */
            handlePreview(file) {
                // console.log(file);
            },
            /*
             文件超出个数限制时的钩子
             */
            handleExceed(files, fileList) {
            },
            /*
            删除文件之前的钩子,参数为上传的文件和文件列表,
            若返回 false 或者返回 Promise 且被 reject,则停止删除
             */
            beforeRemove(file, fileList) {
                // return this.$confirm(`确定移除 ${file.name}?`);
            }
        }
    }
)

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。 

相关文章

  • Vue的props配置项详解

    Vue的props配置项详解

    这篇文章主要为大家详细介绍了Vue的props配置项,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • vue实现图片切换效果

    vue实现图片切换效果

    这篇文章主要为大家详细介绍了vue实现图片切换效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-06-06
  • 浅谈在Vue.js中如何实现时间转换指令

    浅谈在Vue.js中如何实现时间转换指令

    这篇文章主要介绍了浅谈在Vue.js中如何实现时间转换指令,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • vue el-upload 上传文件格式校验方法

    vue el-upload 上传文件格式校验方法

    这篇文章主要介绍了vue el-upload 上传文件格式校验方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-11-11
  • 在vscode里使用.vue代码模板的方法

    在vscode里使用.vue代码模板的方法

    本篇文章主要介绍了在vscode里使用.vue代码模板的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • vue2.0组件之间传值、通信的多种方式(干货)

    vue2.0组件之间传值、通信的多种方式(干货)

    这篇文章主要介绍了vue2.0组件之间传值、通信的多种方式以及注意要点,需要的朋友可以参考下
    2018-02-02
  • vue 遮罩层阻止默认滚动事件操作

    vue 遮罩层阻止默认滚动事件操作

    这篇文章主要介绍了vue 遮罩层阻止默认滚动事件操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • vue实例的选项总结

    vue实例的选项总结

    这篇文章主要介绍了Vue实例的选项有哪些,文中讲解非常细致,代码帮助大家更好的学习,感兴趣的朋友可以了解下
    2020-06-06
  • element el-select下拉框选择失效解决办法

    element el-select下拉框选择失效解决办法

    el-select即选择器用于从若干个候选项中选择其中一个(或者多个),在传统网页开发中选择器经常被称作下拉框、下拉列表是最常用的表单元素之一,这篇文章主要给大家介绍了关于element el-select下拉框选择失效解决办法,需要的朋友可以参考下
    2023-08-08
  • vue.js实现表格合并示例代码

    vue.js实现表格合并示例代码

    最近工作中遇到一个需求,是要做一个页面放张大表格用来显示数据项,纯粹为了view层操作方便,就用了vue做渲染。然而又被提出了一个需求,需要相邻的相同值的行数据项进行单元格合并,这就醉了。没办法,只能想办法解决,下面通过这篇文章来一起看看吧。
    2016-11-11

最新评论