社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

本人使用django1.11.16做了一个在线相册系统,现在想下载选中图片,想要打包下载,打包成zip供别人下载 但是看了网上的帖子很是郁闷。

海盗老哥 • 6 年前 • 310 次点击  

本人使用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调用关闭文件。求大神解答

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/26396
 
310 次点击