• 文件导入功能的前端实现


    1.HTML部分

    <input type="file" accept='.xls,.xlsx' class="file">

    accept属性可以设置要上传文件的格式

    2.js部分

    接口部分

    export function postImportRole(params) {
      return axios.post(servers + '/role/import-roles', params, {
        headers: { 'Content-Type': 'multipart/form-data;charset=UTF-8' }
      });
    }

    代码部分

    // 导入文件
    importFile() {
    const that = this;
    const formData = new window.FormData();
    const files = document.querySelector("input[type=file]").files;
    formData.append("file", files[0]);
    if (files.length <= 0) {
    this.$openMessage("请选择导入文件", "error");
    } else {
    if (!/.(xlsx)$/.test(files[0].name)) {
    this.$openMessage("导入文件格式不正确", "error");
    } else {
    postImportRole(formData)
    .then(res => {
    if (res.data.code === "0") {
    that.visibleImportRole = false;
    this.$openMessage("导入成功");
    this.search();
    } else {
    this.$openMessage(res.data.msg, "error");
    }
    })
    .catch(() =>
    this.$openMessage("导入失败,请核对文档格式是否正确", "error")
    );
    }
    }
  • 相关阅读:
    【CF617D】Roads in Yusland
    对偶问题
    【LG3722】[HNOI2017]影魔
    [HEOI2017] 相逢是问候
    [SHOI2009] 会场预约
    [SCOI2007] 修车
    [CTSC2008] 网络管理
    [国家集训队] 礼物
    [Poetize6] IncDec Sequence
    [网络流24题] 魔术球问题
  • 原文地址:https://www.cnblogs.com/thinkguo/p/11288334.html
Copyright © 2020-2023  润新知