uniapp小程序中处理blob二进制流数据

小程序中是不支持blob格式数据的,new Blob()会报错Blob未定义

处理方法是转成base64格式:

	uni.request({
					url: that.$apiUrl + "/qrcode/generate/" + that.info.fileHash,
						responseType: "arraybuffer",
						method: "get",
					success: function(res) {
						console.log("二维码==>", res);
						if (res.statusCode == 200) {
							let resData = res.data			
							// 生成8位为符号整形数组
							const arrBuf = new Uint8Array(resData)
							const base64 = "data:image/png;base64," + uni.arrayBufferToBase64(arrBuf)
							that.image = base64
						}
					}
				})

首先,请求中要带上responseType: "arraybuffer",保证返回的数据格式是arraybuffer

使用uni.arrayBufferToBase64() 转化成base64格式,后续根据业务处理数据即可。

在这个代码中,数据是一张png格式的二维码图片。

猜你喜欢

转载自blog.csdn.net/Rome_z/article/details/134511350
今日推荐