html
<image @click="chooseImage" :src="pic" class="toux" mode=""></image>
js
chooseImage() {
var _this = this
uni.chooseImage({
count: 1, //默认9
sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], //从相册选择、摄像头
success: function(res) {
_this.imgShow = res.tempFilePaths[0]
_this.uploadFile()
}
});
},
uploadFile(){
var _this = this
uni.uploadFile({
url:this.$baseUrl+`/upload/uploadFile?type=${uni.getStorageSync('userType')}`,
filePath:this.imgShow,
name:'file',
header: {
"Content-Type": "multipart/form-data"
},
success:(res)=>{
var image = JSON.parse(res.data)
this.tou = image.data.imgUrl
this.$myRequest({
url: `/login/uploadAvatar?id=${uni.getStorageSync("userId")}&type=${uni.getStorageSync('userType')}&pic=${this.tou}`,
method:'post',
data:{}
}).then(data=>{
if(data.data.code == 200){
_this.pic = data.data.data.user.pic
uni.setStorageSync("imgs",data.data.data.user.pic)
}
})
}
})
},
uniapp,小程序上传图片
最新推荐文章于 2024-06-06 14:17:58 发布