element-ui中的upload组件带token的两种方法

前端之家收集整理的这篇文章主要介绍了element-ui中的upload组件带token的两种方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

方法1(默认方式):

<el-upload
  action="http://irhfuwu.com:5566/OE/UploadFile/AttachUpload";
  multiple
  :limit="3"
  :headers="myHeaders"    //注:此处也可以使用computed来传动态值
  :on-success="sucH">
  <el-button size="small" type="primary">点击上传1</el-button>
</el-upload>

...

data: {
  myHeaders: {
      token: "324234234234324343pz9cvbg3Rw="
  }
},

方法2(不用action,改用自写ajax方式):

<el-upload
  action="#"
  :before-upload="beforeUpload"
  multiple
  :limit="3"
  :on-success="sucH">
  <el-button size="small" type="primary">beforeUpload方式上传</el-button>
</el-upload>

....

beforeUpload:function (file) {
    var fd = new window.FormData();
    fd.append('key',file,'fileName')
    axios.post("http://irhfuwu.com:5566/OE/UploadFile/AttachUpload";,fd,{
        headers: {
            token: "456436523YZgX6PXALReS5l51QwzVf0eQnYvDJJBKxG91Wpz9cvbg3Rw="
        }
    }).then(function (res) {
      console.log(res);
    });
    return false // 返回false不会自动上传
}

猜你在找的Ajax相关文章