def download_conf_zipfile(request):
import tempfile, zipfile
from django.http import HttpResponse
from django.core.servers.basehttp import FileWrapper
temp = tempfile.TemporaryFile()
archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
src = "/root/test/"
files = os.listdir(src)
for filename in files:
archive.write(src+'/'+filename, filename)
archive.close()
wrapper = FileWrapper(temp)
response = HttpResponse(wrapper, content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename=test.zip'
response['Content-Length'] = temp.tell()
temp.seek(0)
return response
python 下载文件
最新推荐文章于 2024-08-07 07:37:36 发布