栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Django-创建一个包含多个文件的邮政编码,并使其可下载

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Django-创建一个包含多个文件的邮政编码,并使其可下载

我已经在Willy链接到的重复问题上发布了此问题,但是由于无法将包含悬赏的问题作为重复问题解决,因此也可以在此处将其复制:

import osimport zipfileimport StringIOfrom django.http import HttpResponsedef 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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/647788.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号