vue项目中使用element插件中的upload上传文件的踩坑记录

 

 一、项目需求在上传状态中原插件点击直接上传到服务器 本项目中的需求是 点击上传到本地 点击确定按钮之后进行网络请求上传到服务器上

二、解决办法

(1)标签

复制代码
 <span>上传文件:</span>
        <el-upload
          ref="uploadFile"
          class="upload-demo"
          :action="actionUrl"
          :on-change="handleChange"
          :file-list="fileList"
          :show-file-list="true"
          :before-upload="beforeUpload"
          :on-success="successUpdate"
          :auto-upload="false"
          
        >
          <el-button size="small" icon="el-icon-upload2">点击上传</el-button>
        </el-upload>
复制代码

 

(2)js行为

(1)本地上传

复制代码
beforeUpload(file) {
      console.log(file);

      let fileName = file.name;
      // let uid = file.uid
      let pos = fileName.lastIndexOf(".");
      let lastName = fileName.substring(pos, fileName.length);
      console.log(lastName, lastName.toLowerCase());
      if (
        lastName.toLowerCase() !== ".xls" &&
        lastName.toLowerCase() !== ".xlsx"
      ) {
        this.$message.error("文件必须为 .xls .xlsx类型");

        return false;
      }
      return false;
    },
复制代码

(2)点击网络请求上传服务器

transferToManager() {
      console.log('post',this.$refs.uploadFile.$children[0])
      this.$refs.uploadFile.$children[0].post(
        this.$refs.uploadFile.fileList[0].raw
      )
      this.managerStatus = false; 
    },

(3)捕获请求结果

复制代码
 successUpdate(response) {
      console.log('successUpdate',response);
      if (response.code == 200) {
        this.$message(response.msg);
      }else{
         this.$message.warning(response.msg);
      }
      this.getAmountList()
    },
复制代码

三、原因分析

为什么不能直接调用submit呢?因为在befroeupload函数调用时若状态改为false,则会导致submit失效

posted on   樱桃小王子yummy  阅读(1165)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示