elementUI实现上传excel文件并传给后端

我们选择一个按钮来实现上传,点击上传按钮,可从本地选择文件上传,确定后传递给后端。

首先,封装一个按钮:

                    <el-upload
                        style="display: inline-block"
                        action="string"
                        :limit="1"
                        :file-list="fileList"
                        :on-error="loadFileError"
                        :on-success="loadFileSuccess"
                        :before-upload="beforeUpload"
                        accept=".xlsx,.xls"
                        :show-file-list="false"
                        :http-request="uploadFile"
                    ><el-button type="primary" plain>导入关联商品</el-button></el-upload>

我们来看一下这里面的每个属性的作用:

limit: 代表一次可上传的文件数量
file-list: 代表自己定义的属性
on-error: 代表导入文件失败的时候提示的方法
on-success:代表导入文件成功提示的方法
before-upload:代表在上传前检查文件的格式、数据大小、信息等,判定文件是否能够上传
show-file-list:代表是否显示文件列表,false不显示
http-request:本次用来进行上传给后端的方法

接下来是script部分:

定义属性:

export default {
  name: "conferenceSignCard",
  data() {
    return {
      fileList: [], // 导入的文件
    };
  },
}

导入文件失败的提示方法:

    // 导入失败,其中$message为elementui的消息提醒组件
    loadFileError() {
      this.$message({
        message: "文件上传失败!",
        type: "error",
      });
    },

导入文件成功的提示方法:

    loadFileSuccess() {
      this.$message({
        message: "文件上传成功!",
        type: "success",
      });
    },

格式校验:上传前检查文件格式、数据大小等信息,判断是否能够上传。这里可导入xlsx和xls文件格式。

    // 导入前检查文件
    beforeUpload(file) {
      const extension = file.name.split(".")[1] === "xls";
      const extension2 = file.name.split(".")[1] === "xlsx";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!extension && !extension2) {
        this.$message({
          message: "上传模板只能是 xls、xlsx格式!",
          type: "error",
        });
      }
      if (!isLt2M) {
        console.log("上传模板大小不能超过 2MB!");
        this.$message({
          message: "上传模板大小不能超过 2MB!",
          type: "error",
        });
      }
      return extension || extension2 || isLt2M;
    },

传递给后端的方法:

    uploadFile(param){
      const File = param.file;
      let formDataInfo = new FormData();
      formDataInfo.append("file", File);
      loadConferenceFile(formDataInfo).then((res) => {
          this.$message({
            message: res.data,
            type: "success",
          });
      });
    },
loadConferenceFile(formDataInfo){
    return axios.post("/signCard/loadConferenceFile",formDataInfo)
  }

完成!

参考文章:

elementUI加springboot实现上传excel文件给后端并读取excel_前端上传excel文件给后端可以吗-CSDN博客

相关推荐

  1. elementUI实现excel文件

    2024-06-18 12:18:06       9 阅读
  2. 【Web实现文件

    2024-06-18 12:18:06       9 阅读
  3. el-upload图片SpringBoot

    2024-06-18 12:18:06       15 阅读
  4. 前端

    2024-06-18 12:18:06       30 阅读
  5. vue实现excel显示数据

    2024-06-18 12:18:06       50 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-18 12:18:06       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-18 12:18:06       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-18 12:18:06       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-18 12:18:06       20 阅读

热门阅读

  1. 数据库-单表查询-基本查询

    2024-06-18 12:18:06       7 阅读
  2. GDB使用

    2024-06-18 12:18:06       6 阅读
  3. 量产导入 | ATPG_FLOW

    2024-06-18 12:18:06       10 阅读
  4. 【linux 常用命令】

    2024-06-18 12:18:06       7 阅读
  5. 编程从入门到精通:一段跌宕起伏的旅程

    2024-06-18 12:18:06       8 阅读
  6. 所有报表情况查询明细

    2024-06-18 12:18:06       7 阅读
  7. 【C++】内存管理

    2024-06-18 12:18:06       11 阅读
  8. SQL 入门教程

    2024-06-18 12:18:06       6 阅读
  9. Docker容器技术在Linux平台的应用与实践

    2024-06-18 12:18:06       8 阅读
  10. 【MySQL】——概念、逻辑、物理结构设计

    2024-06-18 12:18:06       8 阅读
  11. vue跨域问题,请注意你的项目是vue2还是vue3

    2024-06-18 12:18:06       9 阅读