免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2686 | 回复: 1
打印 上一主题 下一主题

django+python如何完成上传+解压文件的功能 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-06-20 13:53 |只看该作者 |倒序浏览
新手学习
上传功能已经完成,如何解压呢?

code
模版upload.html
  1. <form action="." enctype="multipart/form-data" name="form1" method="post" id="form1">  
  2.             <fieldset class="module aligned ">
  3.                
  4.                 <div class="form-row">
  5.                     <div class="form-row">           
  6.                         {{ form.file.errors }}
  7.                         <label for="id_afile" class="required">上传zip文件:</label> {{form.file}}
  8.                     </div>           
  9.                 </div>
  10.                
  11.             </fieldset>
  12.             <br>
  13.             <div class="submit-row" >
  14.                 <input type="submit" value="保存" class="default" name="_save" onclick="save();"/>
  15.             </div>
  16.         <form>
复制代码
视图views.py
  1. # -*- coding: utf-8 -*-  
  2. from django import forms  
  3. from django.shortcuts import render_to_response  
  4. from code import handle_uploaded_file
  5. from django.http import HttpResponse
  6.    
  7. class UploadFileForm(forms.Form):
  8.     file = forms.FileField(widget=forms.FileInput, required=False)
  9.    
  10. def upload(request):
  11.     if request.method == 'POST':
  12.         form = UploadFileForm(request.POST, request.FILES)
  13.         if form.is_valid():
  14.             handle_uploaded_file(request.FILES['file'])
  15.             html = u"<html><body>Upload Success</body></html>"
  16.             return HttpResponse(html)
  17.     else:
  18.         form = UploadFileForm()
  19.         return render_to_response('upload.html',{'form':form})
复制代码
code.py
  1. from mysite.settings import MEDIA_ROOT
  2. import zipfile
  3. from django.http import HttpResponse

  4. def handle_uploaded_file(f):
  5.     name = "%s" % f.name
  6.     destination = open('%s/%s' % (MEDIA_ROOT, name), 'wb+')
  7.     for chunk in f.chunks():
  8.         destination.write(chunk)
  9.     destination.close()
  10.     unziplog(f)
  11.    
  12. def unziplog(f):
  13.     finallog = ''
  14.     name = "%s" % f.name
  15.     if not zipfile.is_zipfile(name):
  16.         errors = u"<html><body><I>can not unzip the uplog,please make sure the upfile is .zip</I></body></html>"
  17.         return HttpResponse(errors)
  18.         #return False
  19.     try:
  20.         f = zipfile.ZipFile('%s/%s' % (MEDIA_ROOT, name), 'r')
  21.         finallog = f.namelist()[0]
  22.         f.extract(finallog)
  23.     except Exception:
  24.         errors = u"<html><body><I>unzip log file failed</I></body></html>"
  25.         return HttpResponse(errors)
  26.         #return False
  27.     return True
复制代码
可以把文件上传到目录中,如何才能进行解压?

论坛徽章:
0
2 [报告]
发表于 2013-06-21 06:15 |只看该作者
回复 1# wsrwkl1


python-2.7.2-docs-html/library/zipfile.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP