Uniapp上传头像(2024记录)

记录一:

      相册选择图片并上传

         从相册选择图片并上传,使用uni.uploadFile来上传。

uni.chooseImage({
  sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  sourceType: ['album'], //从相册选择,或者使用相机
  success: function (chooseImageRes) {
    const tempFilePaths = chooseImageRes.tempFilePaths
    console.log(tempFilePaths[0])
    uni.uploadFile({
      url: `http://xxxxx`, 
      filePath: tempFilePaths[0],
      name: 'file',
      formData: {
        file: tempFilePaths[0]
      },
      header: {
        // token的参数名称,可在网络请求中查看
        'Jq-Auth': getStorage()
      },
      success: (uploadFileRes) => {
        console.log(uploadFileRes.data)
      }
 })

记录二:

事件参数api:
详见:uni.chooseImage(OBJECT) | uni-app官网 

<view class="chooseImg" @tap="openChooseImg">
	<text v-if="!imgUrl">+</text>
	<image v-else :src="imgUrl" ></image> 
</view>
data(){
	return{
		imgUrl:""
	}
}
/* 上传头像 */
			openChooseImg(){
				uni.chooseImage({
				    count: 1, //默认9
				    sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
				    sourceType: ['album','camera'], //从相册选择,和摄像头功能,默认二者都有
				    success: res=> {
						console.log(res)     
						/*res.tempFilePaths[0]是获取到的第一个数据的blob地址,将他赋值给数据区的imgUrl*/
						this.imgUrl=res.tempFilePaths[0]
						console.log(this.imgUrl)
				    }
				});
			}

记录三:

 

<view class="cleaner-top" @click="chooseImg">
      <view class="cleaner-avatar">
        <image :src="imgArr" mode=""></image>
      </view>
      <view class="cleaner-name">
        点击修改头像
      </view>
    </view>
data() {
      return {
        imgArr:"/static/image/manger_pages/avatar-img.png",
      }
    },

 

methods: {
      // 选择图片
      chooseImg() {
        uni.chooseImage({
        	success: (chooseImageRes) => {
        		const tempFilePaths = chooseImageRes.tempFilePaths;
            // 更新页面的渲染
            this.imgArr = tempFilePaths
            // 上传图片
 
        	}
        })
      },
    }

 

  • 27
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

耀南.

你的鼓励将是我最最最最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值