element-ui+vue单文件上传和 多文件批量上传(多文件走一次接口)

**

单文件上传

**

html

<el-upload
                ref="upload"
                class="upload-demo"
                action="接口地址"
                :data="voiceMeta.uploadParams"
                :on-change="handleFileChange"
                :on-remove="handleFileRemove"
                :on-success="uploadSuccess"
                :on-error="uploadError"
                :before-upload="beforeUploadFile"
                :multiple="false"
                :file-list="voiceMeta.fileList"
                :auto-upload="false"
                :headers="header"
                accept=".MP3, .mp3, .wav, .WAV"
              >
                <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>

data

 header: { 'X-Token': getToken() },
 //getToken()方法自己写

methods


    handleFileChange(file, fileList) {
      console.log('文件改变')
      if (fileList.length > 1) {
        fileList.splice(0, 1)
      }
    },
    handleFileRemove(file, fileList) {
      console.log('文件移除')
    },

    uploadSuccess(response, file, fileList) {
      console.log('上传成功')
      console.log(response)
      if (response.success) {
        this.voiceMeta.fileurl = response.url
        this.$message({
          message: '文件上传成功',
          type: 'success'
        })
        console.log(this.voiceMeta.fileurl)
      }
    },
    uploadError(err, file, fileList) {
      this.$message.error('文件上传失败:' + err.toString())
    },
    beforeUploadFile(file) {
      const temp = file.name.substring(file.name.lastIndexOf('.') + 1)
      if (temp !== 'mp3' && temp !== 'wav' && temp !== 'MP3' && temp !== 'WAV') {
        this.$message({
          message: '当前文件格式不符合要求',
          type: 'error'
        })
        return false
      }
      if (!file) {
        this.$message({
          message: '请选择要上传的文件',
          type: 'error'
        })
        return false
      }
      if (file.size / 1024 / 1024 > 20) {
        this.$message({
          message: '文件大小不能超过20M',
          type: 'error'
        })
        return false
      }
    },
    submitUpload() {
      this.$refs.upload.submit()
    }

上传参数(流)
在这里插入图片描述
**

多文件批量上传走一个接口

**
html

<el-form ref="multi-upload-form" :model="multiUpload" label-width="80px" class="form-container">
                  <el-form-item label="批量导入">
                    <el-upload
                      action="#"
                      :show-file-list="true"
                      :on-remove="removeFile"
                      :multiple="true"
                      :file-list="fileList"
                      :on-change="handleChange"
                      :auto-upload="false"
                      :limit="2"
                      :on-exceed="handleExceed"
                      accept=".MP3, .mp3, .wav, .WAV"
                    >
                      <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                      <el-button style="margin-left: 10px;" size="small" type="success" @click="uploadFile">上传到服务器</el-button>
                      <div slot="tip" class="el-upload__tip">文件不宜超过200</div>
                    </el-upload>
                  </el-form-item>
                </el-form>

js

 data() {
    return {
      fileList: []
    }
  },
 methods: {
    // 选择文件时,往fileList里添加
    handleChange(fileList) {
      this.fileList.push(fileList)
    },
    // 移除文件
    removeFile(file) {
      // 移除文件时,要重新给fileList赋值
      const arr = []
      for (let i = 0; i < this.fileList.length; i++) {
        if (this.fileList[i].uid !== file.uid) {
          arr.push(this.fileList[i])
        }
      }
      this.fileList = arr
    },
    // 手动文件上传
    uploadFile() {
      if (this.fileList.length === 0) {
        this.$message.warning('请选取文件')
        return
      }
      const formData = new FormData()
      // 因为要传一个文件数组过去,所以要循环append
      this.fileList.forEach((file) => {
        formData.append('files', file.raw)
      })
      // batchUploadFile是我自己定义的接口
      batchUploadFile(formData).then(res => {
        if (res.success) {
          // 上传成功的操作
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
        this.fileList = []
      })
    },
    // 上传文件超出个数
    handleExceed(files, fileList) {
      this.$message.warning('文件个数超出限制')
    }
  }

上传时的参数
在这里插入图片描述

  • 8
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
好的,以下是使用element-uivue.js写一段批量删除表格内容的代码: <template> <div> <el-button type="danger" @click="handleDelete">批量删除</el-button> <el-table :data="tableData" ref="multipleTable" :row-key="row => row.id" :show-header="true" :highlight-current-row="true" :default-sort="{prop: 'date', order: 'descending'}"> <el-table-column type="selection" width="55"></el-table-column> <el-table-column prop="date" label="日期" width="180"></el-table-column> <el-table-column prop="name" label="姓名" width="180"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> </div> </template> <script> export default { data() { return { tableData: [{ id: 1, date: '2022-01-01', name: '张三', address: '北京市朝阳区' }, { id: 2, date: '2022-01-02', name: '李四', address: '上海市浦东新区' }, { id: 3, date: '2022-01-03', name: '王五', address: '广州市天河区' }, { id: 4, date: '2022-01-04', name: '赵六', address: '深圳市南山区' }], multipleSelection: [] }; }, methods: { handleDelete() { if (this.multipleSelection.length === ) { this.$message({ message: '请选择要删除的数据', type: 'warning' }); return; } this.$confirm('确认删除选中的数据吗?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => { const ids = this.multipleSelection.map(item => item.id); this.tableData = this.tableData.filter(item => !ids.includes(item.id)); this.multipleSelection = []; this.$message({ message: '删除成功', type: 'success' }); }).catch(() => {}); } } }; </script>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值