wx.getlocalimgdata 怎么循环转base64,单图片上传可以,多张图片就不可以?
发布于 4 年前 作者 wei99 12381 次浏览 来自 问答

jweixin.chooseImage({

						count: checkNumber, // 默认9

						sizeType: \['original', 'compressed'\], // 可以指定是原图还是压缩图,默认二者都有

						sourceType: \['album', 'camera'\], // 可以指定来源是相册还是相机,默认二者都有

						success: function(res) {

							let count = checkNumber <= res.localIds.length ? checkNumber : res.localIds.length

							for (let i = 0; i < count; i++) {

								jweixin.getLocalImgData({

									localId: res.localIds\[i\], // 图片的localID

									success: function(res) {

										const localData = res.localData;

										let imageBase64 = '';

										if (localData.indexOf('data:image') == 0) {

										//苹果的直接赋值,默认生成'data:image/jpeg;base64,'的头部拼接

											imageBase64 = localData;

										} else {

										//此处是安卓中的唯一得坑!在拼接前需要对localData进行换行符的全局替换

										//此时一个正常的base64图片路径就完美生成赋值到img的src中了

											imageBase64 = 'data:image/jpeg;base64,' + localData.replace(/\\n/g, '');

										}

										var blobUrl = window.URL.createObjectURL(\_this.dataURLtoBlob(imageBase64));

										\_this.$myUploadFile({

											header: {},

											filePath: blobUrl

										}).then(function(response) {

											\_this.addProperties(response.imgs\[0\])

										})

									}

								});

 

							}

						}

					});![](https://image.wxopen.club/content_ca899468-cb8d-11eb-99df-001a7dda7111.png)![](https://image.wxopen.club/content_caa15e74-cb8d-11eb-8b39-001a7dda7111.jpg)
1 回复

看起来是该时机没有这个函数,检查下代码是不是时序有问题。

回到顶部