【问题标题】:Generating a file for download without storing a copy locally on the server生成文件以供下载,而不在服务器本地存储副本
【发布时间】:2019-01-08 19:12:07
【问题描述】:

我正在通过 Flask 创建一个 Web 应用程序,它将上传的文件转换为 Base64 并将其存储在一个单独的文件中,然后用户可以下载该文件。 这在存储方面可能非常广泛,因为文件必须在转换后保存,然后用户可以下载它,是否可以创建此文件并将其推送给用户下载,而无需将其存储在本地?

【问题讨论】:

  • 是的,这是可能的。

标签: python flask


【解决方案1】:

您可以在服务器上创建文件并使用Response 对象将它们返回以供下载。

例如,返回一个文本文件(取自现实生活中的网络应用程序):

@app.route('/account/security/2fa_backup_codes.txt')
@login_required
def backup_codes():
    codes = g.user.tfa_backup.replace(',', '\r\n')
    generator = (cell for row in codes for cell in row)
    return Response(generator, mimetype='text/plain',
        headers={'Content-Disposition':
                 'attachment;filename=2fa_backup_codes.txt'})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 2020-11-10
    • 1970-01-01
    • 2012-12-08
    • 2011-12-25
    相关资源
    最近更新 更多