社区所有版块导航
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实现**.zip包下载报错内存使用过大

1277859938 • 9 年前 • 1861 次点击  

各位朋友们好: 我使用django的功能想实现下载一个大的**.zip包的功能,该包大小差不多1个G 网上查了一些方法:本来是准备用遍历文件目录逐个文件打包的,但是我需要打包的文件很多,用这个方法很慢,而且由于我里面有中文文件,遍历文件路径还会报错。因此我选择了,直接我先把所有文件打包到名叫test.zip中,希望用django实现直接下载test.zip包的功能,但是尝试了好几次,都报错:而且保存的原因提示是内存使用过大这种,我看了网上的大文件下载,用的就是下面这个方法: 19.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)
28. filename = “test.zip” # Select your files here.
29. archive.write(filename)
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 希望朋友们能指点一下我这段代码哪里不对,或者有相关直接用django下载zip包的代码也麻烦告知一下。 万分感谢了。

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