nui-app上传图片多张图片转化成base64

<script>
	var sourceType = [
		['camera'], //拍照
		['album'], //相册
		['camera', 'album'] //拍照或相册
	]
	var sizeType = [
		['compressed'], //压缩
		['original'], //原图
		['compressed', 'original'] //压缩或原图
	]
	export default {
		data() {
			return {
			
				sourceTypeIndex: 2,
				sourceType: ['拍照', '相册', '拍照或相册'],
				sizeTypeIndex: 2,
				sizeType: ['压缩', '原图', '压缩或原图'],
				countIndex: 9,
				count: 9,
				imageLength: 9, //限制图片张数
				
			}
			
		}, 
	   
		
			onUnload() {
				    this.imageList = [],
					this.sourceTypeIndex = 2,
					this.sourceType = ['拍照', '相册', '拍照或相册'],
					this.sizeTypeIndex = 2,
					this.sizeType = ['压缩', '原图', '压缩或原图'],
					this.countIndex = 8;
			},
			methods: {
				
				
			
			     //选择图片
			            chooseImage: async function() {
			                uni.chooseImage({
			                    sourceType: sourceType[this.sourceTypeIndex],
			                    // #ifdef MP-WEIXIN
			                    sizeType: sizeType[this.sizeTypeIndex],
			                    // #endif
			                    count: this.imageLength - this.imageList.length,
								
			                    success: (res) => {
									var uploadImgCount = 0; 
			                        const tempFilePaths = res.tempFilePaths;
			                        for (var i = 0, h = tempFilePaths.length; i < h; i++) {   
				                        uni.request({
				                            url: tempFilePaths[i],
				                            method: 'GET',
				                            responseType: 'arraybuffer',
				                            success: ress => {
				                                let base64 = uni.arrayBufferToBase64(ress.data); //把arraybuffer转成base64 
				                                base64 = 'data:image/jpeg;base64,' + base64 //不加上这串字符,在页面无法显示的哦
				                                uni.request({ 
				                                     
													url:this.url+'/im/v1',
													method: 'POST',
													dataType: 'file',
													header: {
														'content-type' : "application/json",
														'Im-Token' : this.ImToken,
														'Im-Client-Id':this.ImClientId,	
														'Im-Device-Number':this.ImDeviceNumber,	
													},
													data:{
														id:this.ImClientId,
														action: 'headPortraitUpload',
														type : '2',
														payload:{
															type:'0',
															head_portrait:base64,
														}
													},
				                                    success: function(uploadFileRes) { 
														   uploadImgCount++;
														   var data = JSON.parse(uploadFileRes.data);
														   imglis.push(data.payload.head_url);
														   this.imglist=imglis;
															uni.setStorage({
																	key: 'image',
																	data: {
																		image: this.imglist,	
																	},	
																});
				                                    }
				                                });
				                            }
				                        })
									}
			
			                    
										return;
									}
			                    }
			                })
			            },
				
	   }
	}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值