错误必须可转换为缓冲区,而不是InMemoryUploadedFile

时间:2016-08-01 15:41:40

标签: django python-2.7 buffer

我正在尝试保存blob图像(jpeg)并收到以下错误:

Traceback (most recent call last):
File "/Users/Shah/Envs/env/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/Shah/Envs/env/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/Shah/Envs/env/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/Shah/Desktop/project/project/views.py", line 604, in thumbnail_photo
image_result.write(blob)
TypeError: must be convertible to a buffer, not InMemoryUploadedFile
[01/Aug/2016 12:20:55] "POST /thumbnail_photo HTTP/1.1" 500 15608

在做了一些谷歌搜索之后,我无法找到解决方案。我不知道如何将InMemoryUploadedFile转换为缓冲区。该方法的相关代码行如下。

    if request.FILES:
        print('files')
        blob = request.FILES['blob']
        image_result = open('what_is_happening.jpeg', 'wb') 
        image_result.write(blob)
        return HttpResponse("success")

谢谢!

1 个答案:

答案 0 :(得分:4)

更改

image_result.write(blob)

image_result.write(blob.read())