我继承下面的Django视图代码,使用由另一个web服务来服务的输出数据的下载版本:“上关闭的文件I/O操作”
def index(request):
# ... (snip) ...
data = base64.decodestring(request.POST['data'])
filename = request.POST['filename']
wrapper = FileWrapper(StringIO(data))
response = HttpResponse(wrapper, content_type=guess_type(str(filename))[0])
response['Content-Length'] = len(data)
response['Content-Disposition'] = "attachment; filename=" + filename
return response
函数本身 - 写入反对Django 1.0 - 升级到1.5后仍然正常工作。不幸的是,覆盖这一观点现在测试失败:
def testDownload(self):
self.client.login(username='test', password='test')
real = 'abc' * 100
data = base64.encodestring(real)
response = self.client.post("/api/v1/download/", {'data': data, 'filename': 'out.jpg'})
self.assertEqual(real, response.content)
self.assertEqual(response['Content-Disposition'], 'attachment; filename=out.jpg')
和错误:
Traceback (most recent call last):
File "/home/fred/.secret_projects/final/gerbils/tests/amf.py", line 548, in testDownload
self.assertEqual(real, response.content)
File "/home/fred/.virtualenvs/cunning_plot/lib/python2.7/site-packages/django/http/response.py", line 282, in content
self._consume_content()
File "/home/carl/.virtualenvs/cunning_plot/lib/python2.7/site-packages/django/http/response.py", line 278, in _consume_content
self.content = b''.join(self.make_bytes(e) for e in self._container)
File "/home/carl/.virtualenvs/cunning_plot/lib/python2.7/site-packages/django/http/response.py", line 278, in <genexpr>
self.content = b''.join(self.make_bytes(e) for e in self._container)
File "/usr/lib64/python2.7/wsgiref/util.py", line 30, in next
data = self.filelike.read(self.blksize)
File "/usr/lib64/python2.7/StringIO.py", line 127, in read
_complain_ifclosed(self.closed)
File "/usr/lib64/python2.7/StringIO.py", line 40, in _complain_ifclosed
raise ValueError, "I/O operation on closed file"
ValueError: I/O operation on closed file
所以..任何想法?我无法看到testDownload()
或index()
中的任何内容,在需要阅读之前必须“关闭”StringIO
。如果有什么事情,它不会影响非测试情况吗?
非常困惑。帮助赞赏。
我不确定什么'FileWrapper'是,但你应该能够做到你想要的没有它。尝试'file_content = StringIO(data)'并将'file_content'传递给'HttpResponse'然后重新运行你的测试。 – abstractpaper
感谢您的评论!它是'django.core.servers.basehttp.FileWrapper',仅供参考。例如参考[这个片段](http://djangosnippets.org/snippets/365/),我认为这就是我的前任使用它的原因。 – supervacuo