文件导入功能的前端实现

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")
);
}
}

转载于:https://www.cnblogs.com/thinkguo/p/11288334.html

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值