您的位置:首页 > Web前端 > Vue.js

vue项目中实现头像上传(axios请求)

2019-04-29 11:56 477 查看

vue项目中做个人资料时需要实现用户头像的更换

html部分

[code]<template>
<div class="dataLeft">
<p class="pic" style="width: 106px">
<img :src="this.$picUrl+userInor.pic" alt="">//src部分是从后端获取的图像,再次不在赘述
</p>
<p class="upload">上传头像
<input type="file" @change="upDataPic" enctype="" accept="image/*" class="uploadBtn"/>
</p>
</div>
</template>

css部分

[code]<style>
.pic{
width:106px;
height:106px;
overflow: hidden;
border-radius:50%;
background:white;
}
.pic img{
width: 100%;
height: 100%;
}
.upload{
font-size:12px;
text-align: center;
line-height: 36px;
}
</style>

js部分

[code]<script>
upDataPic(e){
let file = e.target.files[0];
var picData=new FormData();
picData.append('file',file,file.name);
// console.log(picData)
this.$http({
url:"接口url",
method:'post',
data:picData,
})
.then(response=>{
if(response.data.success==true){
this.$swal.fire({//swal是引用的弹框插件,如需要,可百度查找
text:'上传成功',
icon:'success'
})
this.userIcon=response.data.filePath;
this.getInfor();
}else{
this.$swal.fire({//swal是引用的弹框插件,如需要,可百度查找
text:res.data.msg
})
}
})
.catch(Error=>{

})
}
</script>

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: