wx.arrayBufferToBase64被废弃的替代方案

wx.arrayBufferToBase64被废弃的替代方案

当图片在数据库中以Blob类型存储,以Byte[]类型返回给前端小程序时,需要转换为base64渲染展示,

而wx.arrayBufferToBase64(ArrayBuffer arrayBuffer)从基础库 2.4.0 开始停止维护

替代方案就是使用 base64-js 库来手动实现 ArrayBuffer 到 Base64 的转换。

步骤:

1.使用npm安装

或者从github(https://github.com/beatgammit/base64-js/blob/master/base64js.min.js)获取

可以把base64js.min.js中的内容复制到项目util文件中,命名为base64.js
内容如下:

(function (a) {
  if ("object" == typeof exports && "undefined" != typeof module) module.exports = a();
  else if ("function" == typeof define && define.amd) define([], a);
  else {
    var b;
    b = "undefined" == typeof window ? "undefined" == typeof global ? "undefined" == typeof self ? this : self : global : window, b.base64js = a()
  }
})(function () {
  return function () {
    function b(d, e, g) {
      function a(j, i) {
        if (!e[j]) {
          if (!d[j]) {
            var f = "function" == typeof require && require;
            if (!i && f) return f(j, !0);
            if (h) return h(j, !0);
            var c = new Error("Cannot find module '" + j + "'");
            throw c.code = "MODULE_NOT_FOUND", c
          }
          var k = e[j] = {
            exports: {}
          };
          d[j][0].call(k.exports, function (b) {
            var c = d[j][1][b];
            return a(c || b)
          }, k, k.exports, b, d, e, g)
        }
        return e[j].exports
      }
      for (var h = "function" == typeof require && require, c = 0; c < g.length; c++) a(g[c]);
      return a
    }
    return b
  }()({
    "/": [function (a, b, c) {
      'use strict';

      function d(a) {
        var b = a.length;
        if (0 < b % 4) throw new Error("Invalid string. Length must be a multiple of 4");
        var c = a.indexOf("="); - 1 === c && (c = b);
        var d = c === b ? 0 : 4 - c % 4;
        return [c, d]
      }

      function e(a, b, c) {
        return 3 * (b + c) / 4 - c
      }

      function f(a) {
        var b, c, f = d(a),
          g = f[0],
          h = f[1],
          j = new m(e(a, g, h)),
          k = 0,
          n = 0 < h ? g - 4 : g;
        for (c = 0; c < n; c += 4) b = l[a.charCodeAt(c)] << 18 | l[a.charCodeAt(c + 1)] << 12 | l[a.charCodeAt(c + 2)] << 6 | l[a.charCodeAt(c + 3)], j[k++] = 255 & b >> 16, j[k++] = 255 & b >> 8, j[k++] = 255 & b;
        return 2 === h && (b = l[a.charCodeAt(c)] << 2 | l[a.charCodeAt(c + 1)] >> 4, j[k++] = 255 & b), 1 === h && (b = l[a.charCodeAt(c)] << 10 | l[a.charCodeAt(c + 1)] << 4 | l[a.charCodeAt(c + 2)] >> 2, j[k++] = 255 & b >> 8, j[k++] = 255 & b), j
      }

      function g(a) {
        return k[63 & a >> 18] + k[63 & a >> 12] + k[63 & a >> 6] + k[63 & a]
      }

      function h(a, b, c) {
        for (var d, e = [], f = b; f < c; f += 3) d = (16711680 & a[f] << 16) + (65280 & a[f + 1] << 8) + (255 & a[f + 2]), e.push(g(d));
        return e.join("")
      }

      function j(a) {
        for (var b, c = a.length, d = c % 3, e = [], f = 16383, g = 0, j = c - d; g < j; g += f) e.push(h(a, g, g + f > j ? j : g + f));
        return 1 === d ? (b = a[c - 1], e.push(k[b >> 2] + k[63 & b << 4] + "==")) : 2 === d && (b = (a[c - 2] << 8) + a[c - 1], e.push(k[b >> 10] + k[63 & b >> 4] + k[63 & b << 2] + "=")), e.join("")
      }
      c.byteLength = function (a) {
        var b = d(a),
          c = b[0],
          e = b[1];
        return 3 * (c + e) / 4 - e
      }, c.toByteArray = f, c.fromByteArray = j;
      for (var k = [], l = [], m = "undefined" == typeof Uint8Array ? Array : Uint8Array, n = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", o = 0, p = n.length; o < p; ++o) k[o] = n[o], l[n.charCodeAt(o)] = o;
      l[45] = 62, l[95] = 63
    }, {}]
  }, {}, [])("/")
});

2.可以写一个util.js文件

const base64 = require('base64-js');

function arrayBufferToBase64(arrayBuffer) {
  const uint8Array = new Uint8Array(arrayBuffer);
  const base64Data = base64.fromByteArray(uint8Array);
  return base64Data;
}
module.exports = {
  arrayBufferToBase64
}

3.调用

import util from '../../utils/util'

// 使用示例
const arrayBuffer = /* 获取到的 ArrayBuffer 数据 */;
const base64Data = arrayBufferToBase64(arrayBuffer);
console.log(base64Data);

腾讯云服务器优惠购买
在这里插入图片描述
【腾讯云】语音识别、人脸融合等热门AI产品新用户14.9元起,共享AI领域应用场景和解决方案

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值