ArrayBuffer 到 base64 编码的字符串 [英] ArrayBuffer to base64 encoded string
本文介绍了ArrayBuffer 到 base64 编码的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一种有效的(读取本机)方法将 ArrayBuffer
转换为需要在多部分帖子中使用的 base64 字符串.
I need an efficient (read native) way to convert an ArrayBuffer
to a base64 string which needs to be used on a multipart post.
推荐答案
function _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 );
}
但是,非本地实现更快,例如https://gist.github.com/958841见 http://jsperf.com/encoding-xhr-image-data/6
but, non-native implementations are faster e.g. https://gist.github.com/958841 see http://jsperf.com/encoding-xhr-image-data/6
这篇关于ArrayBuffer 到 base64 编码的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文