代码如下,想在下面的代码里面把文件尺寸发送给前端,求大神指点,谢谢
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