Home > Net >  Why is Django's FileField giving me an encoding error?
Why is Django's FileField giving me an encoding error?

Time:01-16

I have a FileField that saves the file to my file system (Mac) for development, and retrieving this file works with its url, but I get an encoding exception when I try to read it; here's my code:

View:

def download_database(request, id):
    try:
        project = Project.objects.get(id=id)
        with project.database.open('r') as db:
            response = HttpResponse(
                db.read(), content_type="application/vnd.sqlite3, application/x-sqlite3")
            response['Content-Disposition'] = f'inline; filename={project.database.name}'
            return response
    except Project.DoesNotExist:
        raise Http404

HTML template:

            <a href="{{ project.database.url }}">Download</a>
            <a href="{% url 'download_database' project.id %}">Download</a>

Again, the browser downloads the file correctly with the 1st version, but fails with the 2nd, with:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfb in position 106: invalid start byte

... contained in the exception trace:

Internal Server Error: /project/download_database/54553950-15e5-4ea1-999e-8a6ec2a84ffb
Traceback (most recent call last):
  File "/Users/csabbey/code/survey_server/venv/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/csabbey/code/survey_server/venv/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/csabbey/code/survey_server/survey_server/views.py", line 70, in download_database
    db.read(), content_type="application/vnd.sqlite3, application/x-sqlite3")
    ^^^^^^^^^
  File "<frozen codecs>", line 322, in decode
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfb in position 106: invalid start byte
[15/Jan/2023 20:33:01] "GET /project/download_database/54553950-15e5-4ea1-999e-8a6ec2a84ffb HTTP/1.1" 500 90923

This reading works when reading from Google Cloud Storage, which we use for production systems, but not when reading from the development machines' file system. Why is this happening, and how can we fix it?

CodePudding user response:

Open the file in binary mode:

with project.database.open('rb') as db:
    ...
  • Related