JS ArrayBuff to Base64 and Python Base64 to Binary

JS ArrayBuff to Base64

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 );
}

// 读取文件并把文件内容转为 base64 格式

        var reader = new FileReader()
        reader.onload = (function(theFile) {
            return function(e) {
                var name = theFile.name
                var arrayBuffer = new Int8Array(e.target.result)
                var content = _arrayBufferToBase64(arrayBuffer)
                upload_parse_send(name, content)
            }
        })(file)
        reader.readAsArrayBuffer(file)
 

Python Base64 to Binary

    import base64
    with open(fpath, 'wb') as f: 
        bin_data = base64.b64decode(fcontent)
        f.write(bin_data)
点击进入评论 ...