uniapp 上传图片及base64图片上传

 普通的上传图片

<image :src="hear" class="img"></image>
<view @click="change"></view>

change: function() {
	var url = this.Url;
	uni.chooseImage({
		count: 1,
		sizeType: ['original', 'compressed'],
		sourceType: ['album'],
	    success: (chooseImageRes) => {
		    const tempFilePaths = chooseImageRes.tempFilePaths;
			var that=this;
		    uni.uploadFile({
				 url: url + '上传图片的接口',
				 filePath: tempFilePaths[0],
				 name: 'file',
				 formData: {
				     要传的数据
				 },
				 success: (uploadFileRes) => {
				      uni.hideLoading()
				      var infoMessage = JSON.parse(uploadFileRes.data);
				      if (infoMessage.code == 1) {
					        that.imgUrl = infoMessage.data;      // 图片地址
						    this.weburl=this.websiteUrl;         // 域名
						    this.hear=this.weburl+that.imgUrl
				             uni.showToast({
				                title: '上传成功',
				               	icon: 'none',
				             })
				       } else {
				           uni.showToast({
				              title: infoMessage.message,
				              icon: 'none',
				            })
				       }
				}
		})
        }
	})
},

base64     上传多张图片(数组)

<view  v-for="(item,index) in imagearr" :key="index">
	<image class="xst-img100" :src="item"></image>
</view>
import { pathToBase64 , base64ToPath } from 'image-tools'
data() {
	return {
		imgArr: [],
		imagearr: []
    }
},
// 上传图片
upLoadImg: function() {
	var websiteUrl = this.websiteUrl;         //调域名
	var token = uni.getStorageSync('token');  //传 token
	var that = this;
		if (that.imgArr.length > 0) {
			that.imgArr = [];
		}
		console.log(typeof(that.imgArr));
		uni.chooseImage({
			count: 9,
			sizeType: ['original', 'compressed'],
			sourceType: ['album'],
			success: (res) => {
				that.imagearr = res.tempFilePaths;
				for(let i = 0; i < res.tempFilePaths.length; i++){
					pathToBase64(res.tempFilePaths[i])
				.then(base64 => {
					// console.log(base64)
				    that.imgArr.push(base64);
		})
	}				
	}
	});
	},
//其他要上传这些图片的接口
submitInfo: function(types) {
	//console.log(this.imgArr);
	//console.log(typeof(this.imgArr));
	uni.showLoading({
		title: '请稍后',
		mask: true
	})
	uni.request({
		url: this.websiteUrl + '接口',
		data: {
			image: this.imgArr,  //上传上面的那些图片
		    token: uni.getStorageSync('token')
		},
		method: 'POST',
		header: {
			 'content-type': 'application/json',
		},
		success: (res) => {
			console.log(res.data);
			uni.hideLoading();
		    uni.showToast({
				title: res.data.message,
				icon: 'none'
		    })
			if (res.data.code == 1) {
				this.otherAdvice='';
				this.imagearr='';
				this.imgArr=''
		    }
		},fail:(res)=>{
			uni.hideLoading();
		}
	});
}
			

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值