The Python backend reads the binary, base64 encodes it, inserts it into the JSON document, and sends it to the JavaScript interface:
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:
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?
source
share