1

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')

cgdannie
  • 129
  • 1
  • 1
  • 8
  • It looks like your file content is being encoded twice - initially utf-8, then latin-1. I'd imagine this is happening in the browser, but don't have any time to test right now. – snakecharmerb Sep 07 '18 at 06:07
  • @snakecharmerb `binary_data.encode("latin-1")` results in the same error. `binary_data.decode("latin-1")` results in this string `Special chars: ����` which i cannot decode to utf-8. – cgdannie Sep 07 '18 at 12:27
  • Exactly, you can't easily fix this server side - this problem to solve is how is the data getting doubly-encoded (presumably) in the browser. – snakecharmerb Sep 07 '18 at 13:55
  • use base64? readAsDataURL. or better yet: don't. use FormData instead files shouldn't be uploaded/downloaded using json – Endless Sep 07 '18 at 21:52
  • @Endless sounds good. Can you give me a code example for front- and backend? – cgdannie Sep 10 '18 at 21:37

0 Answers0