开发者社区
社区提问意见反馈
开发者钉组织
扫描二维码
加入支付宝开发者钉组织
文档中心
凯发app官方网站的技术支持 & 案例 faq 
支付宝小程序
案例分析
sdk & demo下载
常见问题
常用工具
iot小程序
运维保障
小程序 > 支付宝小程序 > 案例分析 > 小程序中base64数据解码&编码示例
贡献者
收藏
我的文档
设置
本文主要讲解小程序如何实现 base64 数据编码/解码。图片转换成 base64 图片,可通过 canvascontext.todataurl 获取画布指定区域的 data url 数据(获取到的数据就是 base64 字符串)来实现,上传图片建议使用 my.uploadfile
1复制 canvascontext.todataurl 的示例代码放在一个 util 工具文件 base64 中,需要使用时通过 import 引用。小程序实现示例代码:
plain text
复制代码
/**
* 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
*/
2import 引用实现示例:
plain text
复制代码
import {base64} from '/util/base64';
onready(){
let msg = 'hello, base64!夏天是不是到了?我想在汗雨中等你~~_~!';
let encodestr = base64.encode(msg);
console.log(encodestr);
let decodestr = base64.decode(encodestr);
my.alert({
content:decodestr,
success: (res) => {
},
});
console.log(decodestr,decodestr == msg);
}
3提供 java 中 base64 的编码解码示例:
内容没有解决您的问题?您还可以前往 或 寻求帮助
凯发k8官方网娱乐官方 copyright © 2022 支付宝(杭州)信息技术有限公司  | icp证浙b2-20160559
该文档对你是否有帮助?
网站地图