假设“mylist”包含字符串“audio”或“video”或两者。基于“mylist”中的字符串将确定哪些文件被压缩并返回到发出http请求的位置。当我发送我的http请求时,没有下载弹出窗口。我是否错误地将文件路径发送到zip函数?
views.py
from django.http import HttpResponse
from django import http
from zipfile import ZipFile
from StringIO import StringIO
from django.core.servers.basehttp import FileWrapper
def zip (filenames):
buffer= StringIO.StringIO()
z= zipfile.ZipFile( buffer, "w" )
[z.writestr(f) for f in filenames]
z.close()
# generate the file
zippedfiles = HttpResponse(FileWrapper(z), content_type='application/zip')
zippedfiles['Content-Disposition'] = 'attachment; filename=z.zip'
return zippedfiles
def main(request):
if request.method == 'POST':
mylist = request.POST.getlist('list')
files = []
for item in mylist:
if item == 'video':
files.append('/home/dbs/public_html/download/video.html')
elif item == 'audio':
files.append('/home/dbs/public_html/download/audio.html')
zipped = zip(files)
return zipped
else:
return http.HttpResponseForbidden()