'UnicodeDecodeError: 'utf-8' codec can't decode, when uploading from testscript

I am making test script to upload excel file,

def test_upload(self):
    c = Client()
    with open('_material/trick.xlsx') as fp:
        c.post('/cms/template/up', {'name': 'fred','content_file': fp})

There comes the error

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

However in html upload form, there is no error occurs.

class UploadTemplateFileForm(BaseModelForm):
    content_file = forms.FileField(required=True)

Why this difference occurs?



Solution 1:[1]

.xlsx files are binary (they are actually pkzip archives). You must do open('_material/trick.xlsx','rb').

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Roberts