Chrome將zip文件下載爲「download.zip」,firefox將其下載爲「IEtEk_sk.zip.part」並且Internet Explorer甚至不提示保存它,它會嘗試在另一個文件中打開原始內容窗口。 爲什麼在下載在所有瀏覽器中都被保存爲「dbs_custom_library.zip」,甚至在IE中都下載了?django不保留文件名
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>
views.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()
描述內容處置(RFC 2616)的W3C文檔似乎需要在文件名周圍加雙引號。 – geoffspear
我剛剛改變了你所指的這個'壓縮'''Content-Disposition'] ='附件的行; filename =「dbs_custom_library.zip」'''與結果相同 – user1459482