您好,欢迎来到化拓教育网。
搜索
您的当前位置:首页el-upload自定义上传

el-upload自定义上传

来源:化拓教育网
    <el-upload
      ref="upload"
      class="upload-demo"
      :action="''"
      :on-remove="handleRemove"
      :before-upload="beforeUpload"
      :file-list="fileList"
      :http-request="handleFileUpload"
      accept=".xlsx,.XLSX,.xls,.XLS"
      :show-file-list="false"
    >
        <el-button slot="trigger" type="primary" size="mini">
            导入文件</el-button>
        <div slot="tip" class="el-upload__tip">只能上传xlsx/pngxls</div>
    </el-upload>

 :before-upload="beforeUpload"文件上传之前的自定义操作,如再次校验文件格式、大小。。。

   beforeUpload(file) {
      // 如下:在此再次校验文件格式,因为accept虽然会过滤其他文件格式,但若用户强制选择上传其他文件格式时。
      const testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
      const isXlsx = testmsg === 'xlsx' || testmsg === 'XLSX' || testmsg === 'xls' || testmsg === 'XLS'
      if (!isXlsx) {
        this.$message({
          message: '上传文件只能是 xlsx/xls 格式',
          type: 'warning'
        })
        return isXlsx //可以返回 false 阻止文件上传
      }
    },

:http-request="handleFileUpload":覆盖默认的上传行为,实现自定义文件上传函数,接收 `file` 参数。

handleFileUpload(file) {
    const formDate = new FormData()
    formDate.append('file', file.file)
    formDate.append('orderId', this.form.id)//上传的其他参数
    // 上传的接口需要定义Content-type
    axios({
        url:'/api/hasUploadfile',
        data:formDate,
        headers: { 'Content-type': 'multipart/form-data'},
        method: 'post',
    }).then((res) => { 
        this.fileList = []
        this.fileList.push(file.file)
        this.$message.success('上传成功!')
      }).catch((error) => {
        console.log(error, 'error')
        this.$refs.upload.clearFiles()
      })
    },

     :on-remove="handleRemove":删除文件之前的钩子,参数为上传的文件和文件列表,若返回 false 或者返回 Promise 且被 reject,则停止删除。

    handleRemove(file, fileList) {
        this.fileList = fileList
    },

 

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo9.cn 版权所有 赣ICP备2023008801号-1

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务