Pro*_*eus 5 python django python-imaging-library django-rest-framework
我有一个完美的文件上传,但我想为它写一个测试,所以我做了以下....
def test_post_ok(self):
image = Image.new('RGB', (100, 100)
tmp_file = tempfile.NamedTemporaryFile(suffix='.jpg')
image.save(tmp_file)
payload = {
"name": "Test",
"thumbnail_image": tmp_file
}
api = APIClient()
api.credentials(Authorization='Bearer ' + self.token)
response = api.post(url, payload, format='multipart')
Run Code Online (Sandbox Code Playgroud)
但是,测试给出错误...
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1024x768 at 0x108A5DCF8>
{'thumbnail_image': [u'The submitted file is empty.']}
Run Code Online (Sandbox Code Playgroud)
我假设我没有正确地做到这一点,如果不是为什么?
我之前的猜测是错误的.您正在使用具有多部分功能的Rest Framework(非常棒!),因此您可以按原样发送文件,并且文件将以多部分编码.
这里的错误如下:
tmp_filetmp_file.read().由于指针仍在文件的末尾,因此read()将返回0字节,为您留下一个空文档.解决方案:
tmp_file.seek(0)或在调用之前重新打开文件post()
| 归档时间: |
|
| 查看次数: |
950 次 |
| 最近记录: |