uniapp,小程序上传图片

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,小程序,javascript,前端,小程序)