首页 新闻 会员 周边

send_file怎么把文件尺寸发送给前端,可以自定义返回内容吗?或者可以添加头信息返回给前端吗?

0
悬赏园豆:20 [待解决问题]

代码如下,想在下面的代码里面把文件尺寸发送给前端,求大神指点,谢谢
def dl_plan(pid):
"""
下载测试计划关联的测试文件,分单个和多个文件情况(多个文件打包成zip下载)。
1. 计划只有一个文件则直接下载.xlsx文件
2. 计划有多个文件,则打包成zip通过BytesIO直接写入到内存中,不在本地生成.zip文件。
:param pid
:return:
"""

dirpath = Config.FILE_UPLOAD_DIR
tp = TestPlan.objects.get_or_404(id=pid)
fp_test_files = tp.test_files
file_list = fp_test_files.split(",")
dl_name = '{}.zip'.format(tp.planname)

if len(file_list) == 1:
    return send_from_directory(dirpath, fp_test_files, as_attachment=True)
else:
    memory_file = BytesIO()
    with zipfile.ZipFile(memory_file, "w", zipfile.ZIP_DEFLATED) as zf:
        for _file in file_list:
            with open(os.path.join(dirpath, _file), 'rb') as fp:
                zf.writestr(_file, fp.read())
    memory_file.seek(0)
    return send_file(memory_file, attachment_filename=dl_name, as_attachment=True)

尝试参考链接:https://www.helplib.com/Python-Module-Examples/article_62291

奈何技术不达标的主页 奈何技术不达标 | 初学一级 | 园豆:80
提问于:2019-08-16 14:43
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册