Chrome 将 zip 文件下载为“download.zip”,firefox 将其下载为“IEtEk_sk.zip.part”,Internet Explorer 甚至没有提示保存它,它试图在另一个窗口中打开原始内容。为什么在所有浏览器中都没有将下载保存为“dbs_custom_library.zip”,甚至在 IE 中根本没有下载?
html表单
<form action="/django/builder/buildit/" method="POST">
<input type="checkbox" name="list" value="val1" checked/>
<input type="checkbox" name="list" value="val2" checked/>
<input type="checkbox" name="list" value="thisispersian" checked/>
<input type="submit" value="Submit!" />
</form>
视图.py
def zipit (file_paths):
buffer= StringIO.StringIO()
z= zipfile.ZipFile( buffer, "w" )
for p in file_paths:
if os.path.isdir(p):
for dir, subdirs, files in os.walk(p):
for f in files:
filename = os.path.join(dir, f)
z.write(filename, arcname = filename[31:])
else:
z.write(p, arcname = p[31:])
z.close()
buffer.seek(0)
return buffer.getvalue()
def persian (mylist):
files = []
if 'val1' in mylist:
files.append('/home/dbs/public_html/download/Persian/Books/val1.html')
if 'val2' in mylist:
files.append('/home/dbs/public_html/download/Persian/Books/val2.html')
return files
def main(request):
if request.method == 'POST':
mylist = request.POST.getlist('list')
if 'thisispersian' in mylist:
files = persian(mylist)
zipped = zipit(files)
zipped['Content-Disposition'] = 'attachment; filename=dbs_custom_library.zip'
return HttpResponse(zipped, mimetype="application/x-zip-compressed")
else:
return http.HttpResponseForbidden()