base64图像源在Chrome中有效,但在Firefox中无效,到base64的数组缓冲区做错了吗?

时间:2018-11-21 01:53:34

标签: javascript

这是图片https://codepen.io/anon/pen/BGJraW,您可以看到它没有在firefox中加载,但在chrome中加载。

这是我用来转换为base64的过程:

const arrayBufferToBase64 = (buffer) => {
  var binary = ''
  var bytes = new Uint8Array(buffer)
  var len = bytes.byteLength
  for (var i = 0; i < len; i++) {
    binary += String.fromCharCode(bytes[i])
  }
  return window.btoa(binary)
}

const base64Image = 'data:image/png;base64,' + arrayBufferToBase64(entireBuffer)

对于任何我下一步可以尝试的建议,我是开放的,我什至不知道如何开始调试它。

这是我从外部API可读流中获取数组缓冲区的方式:

let entireBuffer
readableStream.on('data', buffer => {
  if (!entireBuffer) {
    entireBuffer = buffer
  } else {
    entireBuffer = concatTypedArrays(entireBuffer, buffer)
  }
})

readableStream.on('end', () => {
  const base64Image = 'data:image/png;base64,' + arrayBufferToBase64(entireBuffer)
})

const concatTypedArrays = (a, b) => {
  var c = new (a.constructor)(a.length + b.length)
  c.set(a, 0)
  c.set(b, a.length)
  return c
}

0 个答案:

没有答案