- 首先要创键FormData
- 然后将传入的参数添加到实例当中
- 最后在请求接口传参
- 代码演示:
vue组件内使用
// my.vue
<van-popup v-model="show" position="bottom">
<ul class="popup-box">
<p>拍照</p>
<p>
// 点击即可调用
<van-uploader
result-type="file" :
after-read="afterRead" > // 选中文件后回调函数
从手机相册选择
</van-uploader>
</p>
<p @click="show0 = false">取消</p>
</ul>
</van-popup>
// config/infex.js
// img 头像 : file:(binary)
img(file){
return request(METHOD.POST,URL.img, file);
},
上传至服务器
// my.vue
afterRead(item) {
//创建FormData 接口只支持FormData接口 传参
let data = new FormData();
data.append("file", item.file);
//弹框显示隐藏
this.show = false;
//请求更换图片的接口
this.$API.img(data).then((res) => {
//修改图片地址
this.$API.user({ avatar:res.data.data.path }).then(res=>{
if(res.data.code == 200){
//重新请求接口获取数据
this.$API.userinfo().then(res=>{
//重新赋值
this.list=res.data.data
})
}else{
//失败后提示弹框
this.$Message(res.data.msg)
}
})
});
},