天地一扁舟 2015-08-03 08:46 采纳率: 0%
浏览 1786

django实现**.zip包下载报错内存使用过大

django实现**.zip包下载报错内存使用过大
各位朋友们好:
我使用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包的代码也麻烦告知一下。
万分感谢了。

  • 写回答

1条回答

  • threenewbee 2015-08-03 22:25
    关注

    先看下网络是否稳定,看看下载下来的数据是不完全还是根本没有下载。

    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果