Python 使用Django生成要下载的文件
是否可以制作zip存档并提供下载,但仍不能将文件保存到硬盘驱动器?是的,您可以使用或其他在内存中创建zip存档。您可以让视图将zip存档写入Django视图返回的Python 使用Django生成要下载的文件,python,django,Python,Django,是否可以制作zip存档并提供下载,但仍不能将文件保存到硬盘驱动器?是的,您可以使用或其他在内存中创建zip存档。您可以让视图将zip存档写入Django视图返回的HttpResponse对象,而不是向模板发送上下文。最后,您需要将mimetype设置为适当的格式。您将更乐意创建一个临时文件。这节省了大量内存。当您同时有一个或两个以上的用户时,您会发现节省内存是非常非常重要的 但是,您可以写入对象 “buffer”对象类似于778字节的ZIP存档文件。要触发下载,您需要设置内容处置标题: from
HttpResponse
对象,而不是向模板发送上下文。最后,您需要将mimetype设置为适当的格式。您将更乐意创建一个临时文件。这节省了大量内存。当您同时有一个或两个以上的用户时,您会发现节省内存是非常非常重要的
但是,您可以写入对象
“buffer”对象类似于778字节的ZIP存档文件。要触发下载,您需要设置
内容处置
标题:
from django.http import HttpResponse
from wsgiref.util import FileWrapper
# generate the file
response = HttpResponse(FileWrapper(myfile.getvalue()), content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename=myfile.zip'
return response
如果不想将文件保存在磁盘上,则需要使用StringIO
import cStringIO as StringIO
myfile = StringIO.StringIO()
while not_finished:
# generate chunk
myfile.write(chunk)
您还可以选择设置内容长度
标题:
response['Content-Length'] = myfile.tell()
models.py
views.py
为什么不制作一个tar文件呢?像这样:
def downloadLogs(req, dir):
response = HttpResponse(content_type='application/x-gzip')
response['Content-Disposition'] = 'attachment; filename=download.tar.gz'
tarred = tarfile.open(fileobj=response, mode='w:gz')
tarred.add(dir)
tarred.close()
return response
def下载(请求、文件名):
文件路径='/'+文件名
fsock=open(文件名和路径为“rb”)
response=HttpResponse(fsock,content_type='application/zip')
响应['Content-Disposition']='附件;filename=myfile.zip'
返回响应
您可以根据需要更换zip和内容类型。与内存tgz存档相同:
import tarfile
from io import BytesIO
def serve_file(request):
out = BytesIO()
tar = tarfile.open(mode = "w:gz", fileobj = out)
data = 'lala'.encode('utf-8')
file = BytesIO(data)
info = tarfile.TarInfo(name="1.txt")
info.size = len(data)
tar.addfile(tarinfo=info, fileobj=file)
tar.close()
response = HttpResponse(out.getvalue(), content_type='application/tgz')
response['Content-Disposition'] = 'attachment; filename=myfile.tgz'
return response
我认为Django Middleware的内容长度可能会自动发生使用此示例下载一个始终为空的文件,有什么想法吗?正如@eleaz28所说,在我的情况下,它也会创建空白文件。我刚刚删除了
FileWrapper
,它成功了。这个答案对Django 1.9不起作用:请看:我在读取模式下打开了文件,然后file.getvalue()给出了属性错误:textiowapper没有属性getvalue。关于节省内存,这一点很好。但是如果使用临时文件,您会将代码放在哪里删除它?@superjoe30:定期清理作业。Django已经有一个管理命令,必须定期运行该命令才能删除旧会话。@superjoe30这就是/tmp的作用:)@s.Lott是否可以使用mod x-sendfile为创建的文件(在您的示例中为z)提供服务?在内存中创建映像大小的字符串看起来不安全。对于较新版本的Django,你应该有content\u type=
而不是mimetype=
你的意思是fsock=open(filePath,“rb”)
在
from django.http import HttpResponse
from StringIO import StringIO
from models import *
import os, mimetypes, urllib
def random_header_image(request):
header = PageHeader.objects.order_by('?')[0]
image = StringIO(file(header.image.path, "rb").read())
mimetype = mimetypes.guess_type(os.path.basename(header.image.name))[0]
return HttpResponse(image.read(), mimetype=mimetype)
def downloadLogs(req, dir):
response = HttpResponse(content_type='application/x-gzip')
response['Content-Disposition'] = 'attachment; filename=download.tar.gz'
tarred = tarfile.open(fileobj=response, mode='w:gz')
tarred.add(dir)
tarred.close()
return response
def download_zip(request,file_name):
filePath = '<path>/'+file_name
fsock = open(file_name_with_path,"rb")
response = HttpResponse(fsock, content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename=myfile.zip'
return response
import tarfile
from io import BytesIO
def serve_file(request):
out = BytesIO()
tar = tarfile.open(mode = "w:gz", fileobj = out)
data = 'lala'.encode('utf-8')
file = BytesIO(data)
info = tarfile.TarInfo(name="1.txt")
info.size = len(data)
tar.addfile(tarinfo=info, fileobj=file)
tar.close()
response = HttpResponse(out.getvalue(), content_type='application/tgz')
response['Content-Disposition'] = 'attachment; filename=myfile.tgz'
return response