vue 使用 el-upload 上传文件(自动上传/手动上传)

vue 使用 el-upload 上传文件(自动上传/手动上传)

1. 自动上传(选择完文件后,调用axios上传)

<el-upload
  ref="upload1"
  action
  :multiple="false"
  accept=".xlsx,.csv,.xls"
  :auto-upload="false"
  :on-change="handleFileChange"
  :show-file-list="false"
>
  <el-button type="success" icon="el-icon-upload2">导入</el-button>
</el-upload>
  • 上传
 // 当开启多选时 filelist有值
 async handleFileChange(file, filelist) {
  this.httpImportFile(file.raw);
},
httpImportFile(file) {
  let formDatas = new FormData();
  formDatas.append("file", file);
  apiImport(formDatas).then((res) => {
      this.$message.success("导入成功");
      this.$refs.upload1.clearFiles();
    }).catch((err) => {});
},

2.手动上传

<el-upload 
    ref="upfile"
    :auto-upload="false"
    :on-change="handleChange"
    :file-list="fileList"
    :multiple="true"
    accept=".xlsx,.csv,.xls"
    action="#">
    <el-button  type="success">选择文件</el-button>
</el-upload>

<el-button  type="success" @click="upload">点击上传</el-button>
  • 上传
export default {
    data(){
        return{
            fileList:[]
        }
    },
    methods:{
        //获得文件列表
         handleChange(file, fileList) {
            this.fileList = fileList;
        },
        upload(){
            let fd = new FormData();
            this.fileList.forEach(item=>{
                //文件信息中raw才是真的文件
                fd.append("files",item.raw);
            })
            apiImport(fd).then(res=>{
                if (res.code === 200) {
                    this.$message('上传成功')
                }else{
                    this.$message('失败')
                }
            })
        },
    }
}
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zsd_666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值