elemenui upload上传文件

1> 接口接收的文件是: file.raw

2>当只能上传一个文件,并自动替换的是判断 fileList.length > 1,并取 fileList.splice(0, 1);

3>一般取值和判断逻辑会在 on-change 里写

4>选取文件和 上传是两个步骤


html完整的代码

<el-upload
    action="https://jsonplaceholder.typicode.com/posts/"
    :on-preview="handlePreview"
    :on-change="handleChange"
    :on-remove="handleRemove"
    :file-list="fileList"
    :auto-upload="false"
    :limit="1"
    :on-exceed="handleExceed">
    <el-button slot="trigger" size="small">选取文件</el-button>
    <el-button size="small" @click="upload">上传</el-button>
    <span class="el-text-red">提示</span>
 </el-upload>

js-所有的绑定函数

    handlePreview(file) {
      console.log(file);
    },
    //on-change事件 取文件 和一些判断
    handleChange(file, fileList) {
      //只限制上传一个文件时,自动替换
      if (fileList.length > 1) {
        fileList.splice(0, 1);
      }  
      // .doc、.docx、.docm
      let_fileType = file.raw.type
      if ([''].includes(_fileType)) {
        this.file = file.raw
        this.param = new FormData()
        this.param.append('files', file.raw);
        
      }
      else {
        this.$message({
          message: '只允许上传后缀名为.doc、.docx、.docm的文件',
          type: 'warning'
        })
        this.fileList = []
      }
    },
    handleRemove(file, fileList) {
      this.param = ''
    },
    handleExceed(files, fileList) {
      this.$message.warning('只能选择一个文件');
    }, 
    //调用上传接口  
    upload() {
      let that = this
      if (that.param == '') {//that.param 存储上传文件的变量
        this.$message({
          message: '请先选择一个文件!',
          type: 'warning',
        })
        return false
      }
      //上传接口 调用上传接口逻辑
      
    },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值