Base64 encoded in python, decoded in javascript

The Python backend reads the binary, base64 encodes it, inserts it into the JSON document, and sends it to the JavaScript interface:

#Python
with open('some_binary_file', 'rb') as in_file:
    return base64.b64encode(in_file.read()).decode('utf-8')

The JavaScript interface extracts a base64 encoded string from a JSON document and turns it into a binary blob:

#JavaScript
b64_string = response['b64_string'];
decoded_file = atob(b64_string);
blob = new Blob([decoded_file], {type: 'application/octet-stream'});

Unfortunately, when loading blob, the encoding seems to be wrong, but I'm not sure where the problem is. For example. This is an Excel file that I can no longer open. In the Python part, I tried different decoders ('ascii', 'latin1'), but that doesn't make any difference. Is there a problem with my code?

+4
source share
1 answer

. JavaScript. , atob base64 . . (LiveScript):

byte_chars = atob base64_str
byte_numbers = [byte_chars.charCodeAt(index) for bc, index in byte_chars]
byte_array = new Uint8Array byte_numbers
blob = new Blob [byte_array], {type: 'application/octet-stream'}
0

Source: https://habr.com/ru/post/1629057/


All Articles