如何在Django中为用户提供动态生成的ZIP存档?
我正在制作一个网站,用户可以选择任意书籍组合并将其下载为ZIP存档.我担心为每个请求生成这样的存档会使我的服务器慢慢爬行.我还听说Django目前没有一个很好的解决方案来提供动态生成的文件.
zgo*_*oda 44
解决方案如下.
使用Python模块zipfile创建zip存档,但由于该文件指定了StringIO对象(ZipFile构造函数需要类似文件的对象).添加要压缩的文件.然后在您的Django应用程序中返回StringIO对象的内容,并将HttpResponse
mimetype设置为application/x-zip-compressed
(或至少application/octet-stream
).如果需要,可以设置content-disposition
标题,但这不应该是真正需要的.
但要注意,在每个请求上创建zip存档是个坏主意,这可能会导致您的服务器崩溃(如果存档很大,则不计算超时).性能方面的方法是在文件系统中的某处缓存生成的输出,并仅在源文件发生更改时重新生成它.更好的想法是提前准备档案(例如通过cron作业)并让你的网络服务器像往常一样提供服务.
dbr*_*dbr 42
这是一个Django视图来做到这一点:
import os
import zipfile
import StringIO
from django.http import HttpResponse
def getfiles(request):
# Files (local path) to put in the .zip
# FIXME: Change this (get paths from DB etc)
filenames = ["/tmp/file1.txt", "/tmp/file2.txt"]
# Folder name in ZIP archive which contains the above files
# E.g [thearchive.zip]/somefiles/file2.txt
# FIXME: Set this to something better
zip_subdir = "somefiles"
zip_filename = "%s.zip" % zip_subdir
# Open StringIO to grab in-memory ZIP contents
s = StringIO.StringIO()
# The zip compressor
zf = zipfile.ZipFile(s, "w")
for fpath in filenames:
# Calculate path for file in zip
fdir, fname = os.path.split(fpath)
zip_path = os.path.join(zip_subdir, fname)
# Add file, at correct path
zf.write(fpath, zip_path)
# Must close zip for all contents to be written
zf.close()
# Grab ZIP file from in-memory, make response with correct MIME-type
resp = HttpResponse(s.getvalue(), mimetype = "application/x-zip-compressed")
# ..and correct content-disposition
resp['Content-Disposition'] = 'attachment; filename=%s' % zip_filename
return resp
Run Code Online (Sandbox Code Playgroud)
Ant*_*ard 18
许多答案在这里建议使用StringIO
或BytesIO
缓冲.但是这不是必需的,因为HttpResponse
它已经是一个类似文件的对象:
response = HttpResponse(content_type='application/zip')
zip_file = zipfile.ZipFile(response, 'w')
for filename in filenames:
zip_file.write(filename)
response['Content-Disposition'] = 'attachment; filename={}'.format(zipfile_name)
return response
Run Code Online (Sandbox Code Playgroud)
对于python3我使用io.ByteIO因为StringIO的被弃用,以实现这一目标.希望能帮助到你.
import io
def my_downloadable_zip(request):
zip_io = io.BytesIO()
with zipfile.ZipFile(zip_io, mode='w', compression=zipfile.ZIP_DEFLATED) as backup_zip:
backup_zip.write('file_name_loc_to_zip') # u can also make use of list of filename location
# and do some iteration over it
response = HttpResponse(zip_io.getvalue(), content_type='application/x-zip-compressed')
response['Content-Disposition'] = 'attachment; filename=%s' % 'your_zipfilename' + ".zip"
response['Content-Length'] = zip_io.tell()
return response
Run Code Online (Sandbox Code Playgroud)
我使用了Django 2.0和Python 3.6.
import zipfile
import os
from io import BytesIO
def download_zip_file(request):
filelist = ["path/to/file-11.txt", "path/to/file-22.txt"]
byte_data = BytesIO()
zip_file = zipfile.ZipFile(byte_data, "w")
for file in filelist:
filename = os.path.basename(os.path.normpath(file))
zip_file.write(file, filename)
zip_file.close()
response = HttpResponse(byte_data.getvalue(), content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename=files.zip'
# Print list files in zip_file
zip_file.printdir()
return response
Run Code Online (Sandbox Code Playgroud)
无耻的插件:你可以使用django-zipview达到同样的目的.
之后pip install django-zipview
:
from zipview.views import BaseZipView
from reviews import Review
class CommentsArchiveView(BaseZipView):
"""Download at once all comments for a review."""
def get_files(self):
document_key = self.kwargs.get('document_key')
reviews = Review.objects \
.filter(document__document_key=document_key) \
.exclude(comments__isnull=True)
return [review.comments.file for review in reviews if review.comments.name]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
24095 次 |
最近记录: |