【问题标题】:Bottle web framework: how to return a csv file to angular clientBottle web 框架:如何将 csv 文件返回给 Angular 客户端
【发布时间】:2026-01-30 13:35:01
【问题描述】:

在 Bottle Web 框架中,我需要返回一个 csv 文件以从 Angular 客户端下载。

@route('/project/download')
def download_projects_result_file():
    #here i have a csv file in /tmp/proj_category.csv

    return ....?

如何将 csv 文件返回给客户端?

谢谢

【问题讨论】:

    标签: python python-3.x flask bottle


    【解决方案1】:

    是在客户端加载文件内容还是直接下载?

    要直接下载,请在瓶子响应中使用Content-Disposition 标头。

    这是一个例子:

    from bottle import LocalResponse, route
    
    @route('/project/download')
    def download_projects_result_file():
        with open('/tmp/proj_category.csv') as file:
            file.seek(0)
            byte_data = file.read()
            response = LocalResponse(
                body=byte_data,
                headers={
                    "Content-Disposition": "attachment; filename='filename.csv'",
                    "Content-Type": "text/csv",
                }
            )
            return response
    

    【讨论】:

    • 并使用 blob 在客户端下载文件内容?
    • 您想简单地将文件内容加载到客户端?还是更多?