I want to upload any file (Word, PDF, Image ...) with an HTML file input to a python server.
With the help of snakecharmerb (Upload file as JSON to Python webserver) I can send the file by encoding it in the following way in JavaScript:
function fileChange(event) {
const file = event.target.files[0];
const fileReader = new FileReader();
fileReader.onload = function(e) {
const encodedFile = btoa(unescape(encodeURIComponent(e.target.result)));
uploadFile(JSON.stringify(encodedFile), file.name);
}
fileReader.readAsText(file);
}
In Python, I can read and save the file in the following way:
import json
from base64 import b64decode
binary_data = b64decode(json.loads(file));
with open(file_name, "wb") as f:
f.write(binary_data)
But the resulting file is not readable or the content is not correct encoded. I think i need to encode the binary_data in utf-8. But binary_data.encode("utf-8")
results in an error.
test_file.txt
Hello World!
Special chars: äöüß
Python error
UnicodeDecodeError('ascii', 'Hello World!\r\nSpecial chars: \xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd', 29, 30, 'ordinal not in range(128)')
I also tried combining it with .decode('string_escape')