下载前保存原始数据时 Zip 文件为空 (python)

时间:2021-03-08 07:19:13

标签: python flask

我正在创建一个要在 Flask 中下载的 zip 文件,该文件在使用 send_file() 函数时可以完美运行。但是当我将zip的原始数据保存到数据库时,打开时返回的zip文件是空的。

  1. 如何临时保存 zip 文件的数据,然后从另一个链接下载?而且,
  2. 与仅将其保存到磁盘并在用户访问链接后擦除它相比,这样做有什么好处吗?
    filename = 'placeholder'
    # Returns bytes data of pdf files in a list
    files = create_pdfs(creds, spreadsheet_id, path)
    memory_file = io.BytesIO()
    with zipfile.ZipFile(memory_file, 'w') as zf:
        for i, individualFile in enumerate(files):
            zf.writestr(filename+f'_{i+1}.pdf', individualFile.getvalue())
    memory_file.seek(0)
    # When using send_file, the files appear as expected
    # return send_file(memory_file, attachment_filename='capsule.zip', as_attachment=True)
    r = init_redis()
    filename = f'{current_user.email}_zip.zip'
    obj = memory_file.getvalue().decode('utf-8', errors='ignore')
    to_save = json.dumps(obj)
    r.set(filename, to_save)     
    # redirects to link to download zip file (zip folder is empty here)
    return redirect(url_for('page.response'))

0 个答案:

没有答案
相关问题