Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 views
in Technique[技术] by (71.8m points)

arrays - Substitute for Uint8Array in javascript?

Since my javascript program is going to upload documents to the server using the FormData object, and the documents are in base64 format, I need to convert a base64 string to a byte array, and this link seems to be the correct one for that:

Convert base64 string to ArrayBuffer:

function _base64ToArrayBuffer(base64) {
    var binary_string = window.atob(base64);
    var len = binary_string.length;
    var bytes = new Uint8Array(len);
    for (var i = 0; i < len; i++) {
        bytes[i] = binary_string.charCodeAt(i);
    }
    return bytes.buffer;
}

Unfortunately, my program depends on an older version of javascript which doesn't define Uint8Array. Does anyone know if there's a alternative way of accomplishing what I want or if there is an alternative to Uint8Array that I can use?

thanks

question from:https://stackoverflow.com/questions/66067474/substitute-for-uint8array-in-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Unluckly it seems that you don't have access to TypedArrays and their buffers, so you can use a simple array.

function _base64ToArray(base64) {
    var binary_string = window.atob(base64);
    var bytes = [];
    for (var i = 0; i < binary_string.length; i++) {
        bytes.push(binary_string.charCodeAt(i));
    }
    return bytes;
}

Using an array is it not the same as a buffer, but I hope this can solve your problem anyway.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...