图片上传,相册选择、相机拍照

<!-- 头像底部弹窗 -->
		<u-action-sheet :actions="list" :show="look" round="20" @select="chooseupload" @close="look=false">
		</u-action-sheet>
<view class="flex-between"
		style="padding:28rpx 0;box-sizing:border-box;border-bottom:  2rpx solid #F6F7FA;">
		<view style="font-size: 30rpx;color: #222222;">
			<text>头像</text>
		</view>
		<view class="flex" style="position:relative;" @click="chooseImage">
			<div class="flex">
				<image style="width: 80rpx;height: 80rpx;border-radius: 8rpx;" :src="userInfo.headimg"
					mode="">
				</image>
				<u-icon name="arrow-right"></u-icon>
			</div>
		</view>
	</view>
    **// 定义数据**
	list: [{
			name: '拍摄',
			color: 'black',
			fontSize: 28
		}, {
			name: '从相册选择',
			color: 'black',
			fontSize: 28
		}, {
			name: '取消',
			color: '#096967',
			fontSize: 28
		}],

        **// 方法**

       // 选择上传图像的方式
		chooseupload(index) {
			console.log("index", index);
			switch (index.name) {
				case "拍摄":
					this.clickUp(1);
					break;
				case "从相册选择":
					this.clickUp(0);
					break;
				default:
					return
					break;
			}
		},
		    // 显示图片
			chooseImage() {
				this.look = true;
			},

			// 上传图片
			clickUp(index) {
				var that = this;

				let type = "";

				switch (index) {
					case 0:
						type = "album";
						break;
					case 1:
						type = "camera";
						break;
					default:
						return
						break;
				}

				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
					sourceType: [type], //拍照还是相册
					success: function(res) {
						uni.showLoading({
							title: "上传中",
							mack: true
						})

						//用户选择或者拍照图片的临时路径
						let tempPath = res.tempFilePaths[0];
						//这里发送上传文件请求,然后服务器响应图片地址然后替换src,然后关闭加载动画
						uni.uploadFile({
							url: app.globalData.apiUrl + 'api/user/upload', //仅为示例,非真实的接口地址
							filePath: tempPath,
							name: 'file',
							formData: {
								'token': uni.getStorageSync('token')
							},
							success: (uploadFileRes) => {

								if (JSON.parse(uploadFileRes.data).code !== 0) {
									uni.$u.toast(JSON.parse(uploadFileRes.data).msg);
								} else {
									commonApi("api/user/update_user", {
										headimg: app.globalData.apiUrl + JSON.parse(
												uploadFileRes.data)
											.data
									}).then((res) => {
										// 刷新个人信息
										that.getPersionInfo();
										// 修改成功提示
										uni.$u.toast("修改成功");
									})
								}

							}
						});
					}
				});
			},
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值