小程序
小游戏
企业微信
微信支付
扫描小程序码分享
https://developers.weixin.qq.com/miniprogram/dev/api/base/wx.arrayBufferToBase64.html
现在小程序既不支持File,也不支持Blob,后端返回的ArrayBuffer类型如何才能转base64呢?
8 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
麻烦描述下具体的需求呢
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
https://www.npmjs.com/package/base64-arraybuffer
管理员发的不能用了,建议自己弄个js文件
不多说直接上代码:
/** * UTF16和UTF8转换对照表 * U+00000000 – U+0000007F 0xxxxxxx * U+00000080 – U+000007FF 110xxxxx 10xxxxxx * U+00000800 – U+0000FFFF 1110xxxx 10xxxxxx 10xxxxxx * U+00010000 – U+001FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx * U+00200000 – U+03FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx * U+04000000 – U+7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */ //外部js引用时这样写:import {Base64} from '/xxx/base64';//路径需要根据实际路径去写 export let Base64 = { // 转码表 tables : [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O' ,'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' ], UTF16ToUTF8 : function (str) { let results = [], len = str.length; for (let i = 0; i < len; i++) { let code = str.charCodeAt(i); if (code > 0x0000 && code <= 0x007F) { /* 一字节,不考虑0x0000,因为是空字节 U+00000000 – U+0000007F 0xxxxxxx */ results.push(str.charAt(i)); } else if (code >= 0x0080 && code <= 0x07FF) { /* 二字节 U+00000080 – U+000007FF 110xxxxx 10xxxxxx 110xxxxx */ let byte1 = 0xC0 | ((code >> 6) & 0x1F); // 10xxxxxx let byte2 = 0x80 | (code & 0x3F); results.push( String.fromCharCode(byte1), String.fromCharCode(byte2) ); } else if (code >= 0x0800 && code <= 0xFFFF) { /* 三字节 U+00000800 – U+0000FFFF 1110xxxx 10xxxxxx 10xxxxxx 1110xxxx */ let byte1 = 0xE0 | ((code >> 12) & 0x0F); // 10xxxxxx let byte2 = 0x80 | ((code >> 6) & 0x3F); // 10xxxxxx let byte3 = 0x80 | (code & 0x3F); results.push( String.fromCharCode(byte1), String.fromCharCode(byte2), String.fromCharCode(byte3) ); } else if (code >= 0x00010000 && code <= 0x001FFFFF) { // 四字节 // U+00010000 – U+001FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx } else if (code >= 0x00200000 && code <= 0x03FFFFFF) { // 五字节 // U+00200000 – U+03FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } else /** if (code >= 0x04000000 && code <= 0x7FFFFFFF)*/ { // 六字节 // U+04000000 – U+7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } } return results.join(''); }, UTF8ToUTF16 : function (str) { let results = [], len = str.length; let i = 0; for (let i = 0; i < len; i++) { let code = str.charCodeAt(i); // 第一字节判断 if (((code >> 7) & 0xFF) == 0x0) { // 一字节 // 0xxxxxxx results.push(str.charAt(i)); } else if (((code >> 5) & 0xFF) == 0x6) { // 二字节 // 110xxxxx 10xxxxxx let code2 = str.charCodeAt(++i); let byte1 = (code & 0x1F) << 6; let byte2 = code2 & 0x3F; let utf16 = byte1 | byte2; results.push(Sting.fromCharCode(utf16)); } else if (((code >> 4) & 0xFF) == 0xE) { // 三字节 // 1110xxxx 10xxxxxx 10xxxxxx let code2 = str.charCodeAt(++i); let code3 = str.charCodeAt(++i); let byte1 = (code << 4) | ((code2 >> 2) & 0x0F); let byte2 = ((code2 & 0x03) << 6) | (code3 & 0x3F); let utf16 = ((byte1 & 0x00FF) << 8) | byte2 results.push(String.fromCharCode(utf16)); } else if (((code >> 3) & 0xFF) == 0x1E) { // 四字节 // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx } else if (((code >> 2) & 0xFF) == 0x3E) { // 五字节 // 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } else /** if (((code >> 1) & 0xFF) == 0x7E)*/ { // 六字节 // 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } } return results.join(''); }, encode : function (str) { if (!str) { return ''; } let utf8 = this.UTF16ToUTF8(str); // 转成UTF-8 let i = 0; // 遍历索引 let len = utf8.length; let results = []; while (i < len) { let c1 = utf8.charCodeAt(i++) & 0xFF; results.push(this.tables[c1 >> 2]); // 补2个= if (i == len) { results.push(this.tables[(c1 & 0x3) << 4]); results.push('=='); break; } let c2 = utf8.charCodeAt(i++); // 补1个= if (i == len) { results.push(this.tables[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]); results.push(this.tables[(c2 & 0x0F) << 2]); results.push('='); break; } let c3 = utf8.charCodeAt(i++); results.push(this.tables[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]); results.push(this.tables[((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6)]); results.push(this.tables[c3 & 0x3F]); } return results.join(''); }, decode : function (str) { //判断是否为空 if (!str) { return ''; } let len = str.length; let i = 0; let results = []; //循环解出字符数组 while (i < len) { let code1 = this.tables.indexOf(str.charAt(i++)); let code2 = this.tables.indexOf(str.charAt(i++)); let code3 = this.tables.indexOf(str.charAt(i++)); let code4 = this.tables.indexOf(str.charAt(i++)); let c1 = (code1 << 2) | (code2 >> 4); results.push(String.fromCharCode(c1)); if (code3 != -1) { let c2 = ((code2 & 0xF) << 4) | (code3 >> 2); results.push(String.fromCharCode(c2)); } if (code4 != -1) { let c3 = ((code3 & 0x3) << 6) | code4; results.push(String.fromCharCode(c3)); } } return this.UTF8ToUTF16(results.join('')); } };
可以直接复制上面代码放在一个util工具文件base64中,需要使用是使用import引用
在这里给个引用示例:
//在需要加、解码的的文件里引入【注意替换成自己的路径】 import {Base64} from '/util/base64'; let msg = 'Hello, base64!我是威叔~~_~!'; let encodeStr = Base64.encode(msg); //加密 console.log(encodeStr); let decodeStr = Base64.decode(encodeStr);//解码 console.log(decodeStr);
//获取二进制数据 let buffer = resp.result.data.buffer //将二进制数据转成内存地址 let bufferURL = wx.createBufferURL(buffer) //下载图片 wx.downloadFile({ url: bufferURL, success:(res)=> { if (res.statusCode === 200) { this.setData({ image:res.tempFilePath }) } } })
const fs = uni.getFileSystemManager() const wxFsWriteFile = wxPromisify(fs.writeFile) const wxFsReadFile = wxPromisify(fs.readFile) const base64ByFs = { base64TempFile: (wx as any).env.USER_DATA_PATH + `/base64Temp`, async encode(buf: ArrayBufferLike) { const getDeltaMs = createDeltaMs() await wxFsWriteFile({ filePath: this.base64TempFile, data: buf, }) const d = await wxFsReadFile({ filePath: this.base64TempFile, encoding: 'base64' }) as any logger.log('base64ByFs encode', getDeltaMs()) return d.data }, async decode(str: string) { const getDeltaMs = createDeltaMs() await wxFsWriteFile({ filePath: this.base64TempFile, data: str, encoding: 'base64', }) const d = await wxFsReadFile({ filePath: this.base64TempFile, }) as any logger.log('base64ByFs decode', getDeltaMs()) return d.data } }
使用base64-js就好, 在dependencies中加上"base64-js": "^1.5.1", 构建下npm即可
var base64js = require('base64-js'); // 使用base64-js进行Base64编码 var base64Data = base64js.fromByteArray(view); // 使用base64-js进行Base64解码 var decodedData = base64js.toByteArray(base64Data); var decodedArrayBuffer = Uint8Array.from(decodedData).buffer;
我是全栈 其实可以后端转成base64 直接传过来就可以 亲测可以的。
可以把arrayBuffer转化为UTF-8编码的字符串,参考链接:https://github.com/nfroidure/utf-8,亲测有效。
//先安装npm npm install utf-8 //安装完别忘了工具->构建npm //在需要用到的地方导入 const UTF8 = require('../../miniprogram_npm/utf-8/index') Page({ ... }) //ArrayBuffer转UTF-8 let string = UTF8.getStringFromBytes([49, 46, 51, 36, 32, 126, ...]); //括号中要传入array数组格式的字节数据 //ArrayBuffer to array let arrayBuffer = new ArrayBuffer(10); let array = Array.prototype.slice.call(new Uint8Array(arrayBuffer)); //UTF-8转ArrayBuffe let arrayBuffer = UTF8.setBytesFromString('1.3$ ~= 1€', 2, null, true);
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
麻烦描述下具体的需求呢
https://www.npmjs.com/package/base64-arraybuffer
管理员发的不能用了,建议自己弄个js文件
不多说直接上代码:
/** * UTF16和UTF8转换对照表 * U+00000000 – U+0000007F 0xxxxxxx * U+00000080 – U+000007FF 110xxxxx 10xxxxxx * U+00000800 – U+0000FFFF 1110xxxx 10xxxxxx 10xxxxxx * U+00010000 – U+001FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx * U+00200000 – U+03FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx * U+04000000 – U+7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */ //外部js引用时这样写:import {Base64} from '/xxx/base64';//路径需要根据实际路径去写 export let Base64 = { // 转码表 tables : [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O' ,'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' ], UTF16ToUTF8 : function (str) { let results = [], len = str.length; for (let i = 0; i < len; i++) { let code = str.charCodeAt(i); if (code > 0x0000 && code <= 0x007F) { /* 一字节,不考虑0x0000,因为是空字节 U+00000000 – U+0000007F 0xxxxxxx */ results.push(str.charAt(i)); } else if (code >= 0x0080 && code <= 0x07FF) { /* 二字节 U+00000080 – U+000007FF 110xxxxx 10xxxxxx 110xxxxx */ let byte1 = 0xC0 | ((code >> 6) & 0x1F); // 10xxxxxx let byte2 = 0x80 | (code & 0x3F); results.push( String.fromCharCode(byte1), String.fromCharCode(byte2) ); } else if (code >= 0x0800 && code <= 0xFFFF) { /* 三字节 U+00000800 – U+0000FFFF 1110xxxx 10xxxxxx 10xxxxxx 1110xxxx */ let byte1 = 0xE0 | ((code >> 12) & 0x0F); // 10xxxxxx let byte2 = 0x80 | ((code >> 6) & 0x3F); // 10xxxxxx let byte3 = 0x80 | (code & 0x3F); results.push( String.fromCharCode(byte1), String.fromCharCode(byte2), String.fromCharCode(byte3) ); } else if (code >= 0x00010000 && code <= 0x001FFFFF) { // 四字节 // U+00010000 – U+001FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx } else if (code >= 0x00200000 && code <= 0x03FFFFFF) { // 五字节 // U+00200000 – U+03FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } else /** if (code >= 0x04000000 && code <= 0x7FFFFFFF)*/ { // 六字节 // U+04000000 – U+7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } } return results.join(''); }, UTF8ToUTF16 : function (str) { let results = [], len = str.length; let i = 0; for (let i = 0; i < len; i++) { let code = str.charCodeAt(i); // 第一字节判断 if (((code >> 7) & 0xFF) == 0x0) { // 一字节 // 0xxxxxxx results.push(str.charAt(i)); } else if (((code >> 5) & 0xFF) == 0x6) { // 二字节 // 110xxxxx 10xxxxxx let code2 = str.charCodeAt(++i); let byte1 = (code & 0x1F) << 6; let byte2 = code2 & 0x3F; let utf16 = byte1 | byte2; results.push(Sting.fromCharCode(utf16)); } else if (((code >> 4) & 0xFF) == 0xE) { // 三字节 // 1110xxxx 10xxxxxx 10xxxxxx let code2 = str.charCodeAt(++i); let code3 = str.charCodeAt(++i); let byte1 = (code << 4) | ((code2 >> 2) & 0x0F); let byte2 = ((code2 & 0x03) << 6) | (code3 & 0x3F); let utf16 = ((byte1 & 0x00FF) << 8) | byte2 results.push(String.fromCharCode(utf16)); } else if (((code >> 3) & 0xFF) == 0x1E) { // 四字节 // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx } else if (((code >> 2) & 0xFF) == 0x3E) { // 五字节 // 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } else /** if (((code >> 1) & 0xFF) == 0x7E)*/ { // 六字节 // 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } } return results.join(''); }, encode : function (str) { if (!str) { return ''; } let utf8 = this.UTF16ToUTF8(str); // 转成UTF-8 let i = 0; // 遍历索引 let len = utf8.length; let results = []; while (i < len) { let c1 = utf8.charCodeAt(i++) & 0xFF; results.push(this.tables[c1 >> 2]); // 补2个= if (i == len) { results.push(this.tables[(c1 & 0x3) << 4]); results.push('=='); break; } let c2 = utf8.charCodeAt(i++); // 补1个= if (i == len) { results.push(this.tables[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]); results.push(this.tables[(c2 & 0x0F) << 2]); results.push('='); break; } let c3 = utf8.charCodeAt(i++); results.push(this.tables[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]); results.push(this.tables[((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6)]); results.push(this.tables[c3 & 0x3F]); } return results.join(''); }, decode : function (str) { //判断是否为空 if (!str) { return ''; } let len = str.length; let i = 0; let results = []; //循环解出字符数组 while (i < len) { let code1 = this.tables.indexOf(str.charAt(i++)); let code2 = this.tables.indexOf(str.charAt(i++)); let code3 = this.tables.indexOf(str.charAt(i++)); let code4 = this.tables.indexOf(str.charAt(i++)); let c1 = (code1 << 2) | (code2 >> 4); results.push(String.fromCharCode(c1)); if (code3 != -1) { let c2 = ((code2 & 0xF) << 4) | (code3 >> 2); results.push(String.fromCharCode(c2)); } if (code4 != -1) { let c3 = ((code3 & 0x3) << 6) | code4; results.push(String.fromCharCode(c3)); } } return this.UTF8ToUTF16(results.join('')); } };
可以直接复制上面代码放在一个util工具文件base64中,需要使用是使用import引用
在这里给个引用示例:
//在需要加、解码的的文件里引入【注意替换成自己的路径】 import {Base64} from '/util/base64'; let msg = 'Hello, base64!我是威叔~~_~!'; let encodeStr = Base64.encode(msg); //加密 console.log(encodeStr); let decodeStr = Base64.decode(encodeStr);//解码 console.log(decodeStr);
//获取二进制数据 let buffer = resp.result.data.buffer //将二进制数据转成内存地址 let bufferURL = wx.createBufferURL(buffer) //下载图片 wx.downloadFile({ url: bufferURL, success:(res)=> { if (res.statusCode === 200) { this.setData({ image:res.tempFilePath }) } } })
const fs = uni.getFileSystemManager() const wxFsWriteFile = wxPromisify(fs.writeFile) const wxFsReadFile = wxPromisify(fs.readFile) const base64ByFs = { base64TempFile: (wx as any).env.USER_DATA_PATH + `/base64Temp`, async encode(buf: ArrayBufferLike) { const getDeltaMs = createDeltaMs() await wxFsWriteFile({ filePath: this.base64TempFile, data: buf, }) const d = await wxFsReadFile({ filePath: this.base64TempFile, encoding: 'base64' }) as any logger.log('base64ByFs encode', getDeltaMs()) return d.data }, async decode(str: string) { const getDeltaMs = createDeltaMs() await wxFsWriteFile({ filePath: this.base64TempFile, data: str, encoding: 'base64', }) const d = await wxFsReadFile({ filePath: this.base64TempFile, }) as any logger.log('base64ByFs decode', getDeltaMs()) return d.data } }
使用base64-js就好, 在dependencies中加上"base64-js": "^1.5.1", 构建下npm即可
var base64js = require('base64-js'); // 使用base64-js进行Base64编码 var base64Data = base64js.fromByteArray(view); // 使用base64-js进行Base64解码 var decodedData = base64js.toByteArray(base64Data); var decodedArrayBuffer = Uint8Array.from(decodedData).buffer;
我是全栈 其实可以后端转成base64 直接传过来就可以 亲测可以的。
可以把arrayBuffer转化为UTF-8编码的字符串,参考链接:https://github.com/nfroidure/utf-8,亲测有效。
//先安装npm npm install utf-8 //安装完别忘了工具->构建npm //在需要用到的地方导入 const UTF8 = require('../../miniprogram_npm/utf-8/index') Page({ ... }) //ArrayBuffer转UTF-8 let string = UTF8.getStringFromBytes([49, 46, 51, 36, 32, 126, ...]); //括号中要传入array数组格式的字节数据 //ArrayBuffer to array let arrayBuffer = new ArrayBuffer(10); let array = Array.prototype.slice.call(new Uint8Array(arrayBuffer)); //UTF-8转ArrayBuffe let arrayBuffer = UTF8.setBytesFromString('1.3$ ~= 1€', 2, null, true);