我正在开发一个下载图像的功能。
但我正在解决此错误
coercing to Unicode: need string or buffer, ImageFieldFile found
代码:
def download_image(request, image_id):
img = Base.objects.get(base_id=image_id)
wrapper = FileWrapper(open(img.file)) # img.file returns full path to the image
content_type = mimetypes.guess_type(filename)[0] # Use mimetypes to get file type
response = HttpResponse(wrapper,content_type=content_type)
response['Content-Length'] = os.path.getsize(img.file)
response['Content-Disposition'] = "attachment; filename=%s" % img.name
return response
答案 0 :(得分:0)
img.file
是ImageFieldFile
个对象,open()
需要一个字符串。您需要open(img.file.name)
来获取实际的文件路径。