Uniapp微信授权头像

该代码段展示了在uni-app环境下,如何在微信小程序中实现选择头像并上传到服务器的功能。当用户点击按钮时,触发`onChooseAvatar`函数,获取头像URL,然后调用`uni.uploadFile`将图片上传到指定接口。成功后在控制台打印返回的数据。
摘要由CSDN通过智能技术生成
			<!-- #ifdef MP-WEIXIN -->
			<button class="share-button" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
				<view>
					<image src="{{avatar}}" style="width: 140rpx;height: 140rpx;border-radius: 50%;"/>
				</view>
			</button>
			<!-- #endif -->

		  onChooseAvatar(e) {
				let that = this
				const {
					avatarUrl
				} = e.detail
				uni.uploadFile({
					url: '后台上传图片接口',
					filePath: avatarUrl,
					fileType: 'image',
					name: 'file',
					headers: {
						'Accept': 'application/json',
						'Content-Type': 'multipart/form-data',
					},
					formData: {
						'upfile': avatarUrl
					},
					success: (uploadFileRes) => {
						let rest = JSON.parse(uploadFileRes.data)
						console.log(rest)
					},
					fail: (error) => {
						console.log(error)
					},
				})
			},
			

			/**
			 * 去掉按钮样式
			 */	
			.share-button {
				outline: none;
				border: none;
				margin: 0;
				padding: 0;
				background-color: #FFFFFF;
			}
			
			.share-button::after {
				outline: none;
				border: none;
			}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值