我正在使用Django REST進行文件上傳測試。
Python3.6.2
Django1.11
djangorestframework == 3.6.4
Excel的OSX 15.38(170902)
OSX 10.12.6
UnicodeDecodeError:'utf-8'編解碼器無法解碼位置14中的字節0xb9:無效起始字節
它曾經被與普通照片文件做成功。
這次是來自網站的Excel文件。這是我的參考文獻的測試用例副本。
def test_upload_and_process_data_complete_case(self):
from django.core.files import File
from django.core.files.uploadedfile import SimpleUploadedFile
from soken_web.apps.imported_files.models import ImportFile
file = File(open(str(settings.BASE_DIR) + '/apps/zipcodes/complete.xlsx'))
uploaded_file = SimpleUploadedFile('new_image.xlsx', file.read(), content_type='multipart/form-data')
data = {
'attribute': {'author': 'Sigh'},
'type': ImportFile.FileType.zipcode,
'file': uploaded_file
}
response = self.client.post(reverse('api:import_file-list'), data, format='multipart')
response.render()
self.assertEqual(status.HTTP_201_CREATED, response.status_code)
就像一隻副本貓。除此之外,我從https://www.mockaroo.com/下載了一個模擬文件。
以下是錯誤引發當我執行file.read()
file
<File: /Users/el/Code/norak-cutter/soken/soken-web/soken_web/apps/zipcodes/complete.xlsx>
file.read()
Traceback (most recent call last):
File "/Users/el/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/172.3968.37/PyCharm.app/Contents/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
exec(exp, global_vars, local_vars)
File "<input>", line 1, in <module>
File "/Users/el/.pyenv/versions/3.6.2/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb9 in position 14: invalid start byte
確認書:
1.我可以從我的網頁瀏覽器
2.上傳文件,我可以打開該文件沒有任何警告消息。
問:
是否有什麼我忘了關注?
參考文獻:
How can I test binary file uploading with django-rest-framework's test client?
Django REST UnitTest No file was submitted
AssertionError:測試數據包含鍵'屬性'的字典值,但分段上傳不支持嵌套數據。你可能要考慮在這個測試用例中使用format ='json'。 – Sarit
這是一個不同的問題...因爲我感覺慈善,你應該看看https://stackoverflow.com/a/11170472/2372812 –
我曾嘗試複製內容並粘貼在OSX上的Excel。我得到了不同的數字而不是'0xb9'成爲'0x8e' .... – Sarit