如何使文件在 FastAPI 中可下载并支持多种扩展类型?

问题描述 投票:0回答:0

我正在从服务器获取文件内容。服务器包含所有类型的文件,如 json、文本等。

@app.get("myurl")
def get_file(myfilename):
  mydata=fetch_file_from_server(myfilename)
  headers = {"content-disposition": "attachment; filename=\"{}\"".format(myfilename)}
  return Response(content=mydata, media_type="application/json", headers=headers)

我试过

media_type="application/json"
但它似乎只适用于 JSON 文件。

我希望无论扩展名如何都能下载文件。

我目前正在使用

media_type="application/tar+gzip"
,但不确定这是否是正确的方法。

python fastapi httpresponse mime-types media-type
© www.soinside.com 2019 - 2024. All rights reserved.