本人使用django1.11.16做了一个在线相册系统,现在想下载选中图片,想要打包下载,打包成zip供别人下载
但是看了网上的帖子很是郁闷。
def send_zipfile(request):
20 """
21 Create a ZIP file on disk and transmit it in chunks of 8KB,
22 without loading the whole file into memory. A similar approach can
23 be used for large dynamic PDF files.
24 """
25 temp = tempfile.TemporaryFile()
26 archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
27 for index in range(10):
28 filename = file # Select your files here.
29 archive.write(filename, 'file%d.txt' % index)
30 archive.close()
31 wrapper = FileWrapper(temp)
32 response = HttpResponse(wrapper, content_type='application/zip')
33 response['Content-Disposition'] = 'attachment; filename=test.zip'
34 response['Content-Length'] = temp.tell()
35 temp.seek(0)
36 return response
这里边我真是不懂archive.write(filename, 'file%d.txt' % index)为什么要加txt这句。
另外我运行就报错说是I/O调用关闭文件。求大神解答