vue-element上传组件,请求头加入token,上传格式判断

<template>
 <el-upload
      class="upload-demo"
      action="接口地址"
      :before-upload="beforeAvatarUpload"//上传前文件判断
      :on-success="handlesuccess"//上传成功调用
      :headers="headers"//请求头添加
    >
      <el-button type="primary" size="small">学员导入</el-button>
    </el-upload>
</template>

<script>
export default {
//给求头加入token值
  computed: {
    headers() {
      return {
        Authorization: 'token值'
      };
    }
  },
  methods:{
    beforeAvatarUpload(file) {
    console.log(file)
    //输出file可以看到上传的文件的相关信息,这里只写了格式的判断,还可以判断限制文件大小,方法相同
    //上传之前判断格式
       const type =
        file.type === "application/msword" ||
        file.type ===
          "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ||
        file.type === "text/plain" ||
        file.type ===
          "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ||
        file.type === "application/vnd.ms-excel" ||
        file.type === "application/vnd.ms-powerpoint" ||
        file.type ===
          "application/vnd.openxmlformats-officedocument.presentationml.presentation" ||
        file.type === "application/pdf";

      if (!type) {
        this.$message.error("请上传正确的文件格式!");
      }
      return type;
    },
    //上传成功调用方法
    handlesuccess(file) {
      if (file.msg == "上传成功") {
      } else {
        this.$message.error("上传文件失败");
      }
    },
  }
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值