0

I am uploading files in my Django application, which are recorded on the hard drive, and at a later moment they are retrieved. This works well for most files. However, every once in a while there is a file -- generally a PDF file -- that can't be retrieved properly. This is the error that comes up:

UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 194: ordinal not in range(128)

I have seen other questions about this encoding issue, which all relate to how to deal with this when encoding plain text, but I am dealing with binary files. Here is my code:

Relevant upload code:

with open(path, "wb+") as destination:
    for chunk in attachment.chunks():
        destination.write(chunk)

Code to retrieve the file:

with open(file_path, "rb") as f:
    contents = f.read()
response = HttpResponse(contents, content_type="application")
response["Content-Disposition"] = "attachment; filename=\""+name + "\""

I understand there is an encoding issue, but where exactly should I fix this?

  • Which Python version are you using? Maybe This helps? https://stackoverflow.com/questions/147741/character-reading-from-file-in-python – Torben545 Jul 23 '20 at 12:20
  • Can you read the pdf from a console script (no django) ? – Mike67 Jul 23 '20 at 12:25
  • Try with `content_type='application/octet-stream'`, but are you sure the error is in such line? – Giacomo Catenazzi Jul 23 '20 at 12:28
  • @Torben545: I am using Python 3.6.9 -- I'd had a look at that other question but I'm not sure exactly how to embed it. I tried it with the file reading part but didn't succeed. –  Jul 23 '20 at 13:11
  • @Mike67: Yes, when I open the PDF file using other applications (e.g. a regular PDF viewer) then it opens just fine. Not sure how to open it from a console script but I just downloaded it and tried it and that worked just fine. –  Jul 23 '20 at 13:12
  • @GiacomoCatenazzi: I tried that, doesn't work. I'm not totally sure because Django doesn't give me a normal error and I have to check syslog, where I can't see the proper line where this error is generated. So I'm not totally sure but there isn't any other reading/writing of files happening. –  Jul 23 '20 at 13:15
  • I have some doubts that the problem is in that line. HttpResponse accebt bytestrings. Instead of comment, put `"abcd"`, to see if the error is in `response =` line, then try removinf read()... etc. until you see the starting point (obviously do a backup file) – Giacomo Catenazzi Jul 23 '20 at 13:24

0 Answers0