繁体   English   中英

Flask发送文件而不存储在服务器上

[英]Flask send file without storing on server

我希望用户能够下载包含sql查询结果的txt文件。 我已经看到有关使用send_fileResponse的答案,但是所有这些答案似乎都要求我存储了文件?

目前我有:

@RateRevisionEndorsements_blueprint.route('/_getEndorsements', methods = ['GET'])
def get_endorsements():
    guid = request.args.get('guid')


    client = Client()

    # Save query results    
    result = client.getEndorsementFile(bookGuid = guid)


    with open('tesult.txt', 'w') as r:
        for i in result:
            r.write(i)

    return send_file("result.txt", as_attachment=True)

生成此路由的按钮有效,并且我没有收到查询结果的问题(当前存储为列表,但我可以使其尽其所能),但是我收到错误FileNotFoundError: [Errno 2] No such file or directory: 'C"\\\\..\\\\app\\\\result.txt'这使我认为我需要将其存储在服务器上的某个位置以进行提取。

只需将数据作为流响应发送即可。 确保设置正确的mime类型,以便浏览器启动下载。

正是send_file发送了存储的文件。 与发送

Respone(file,mimetype=“txt/plain”)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM