我使用烧瓶路径作为代理下载文件,如下所示:
@esa_handler.route("/data/<int:series>/<int:file_num>", methods=["GET"])
def DownloadRemote(series, file_num):
"""
Downloads the remote files from the ESA.
:param series: 0-20.
:param file_num: File within the series, 0-255
:return: Compressed CSV file.
"""
# if the file is bad.
if series >= 20 and file_num > 110:
return jsonify({"error": "file does not exist."})
url = "http://cdn.gea.esac.esa.int/Gaia/gaia_source/csv/GaiaSource_000-{:03d}-{:03d}.csv.gz".format(series,
file_num)
req = requests.get(url, stream=True)
return Response(stream_with_context(req.iter_content(chunk_size=2048)), content_type=req.headers["content-type"])
它工作得很好,但是,提供给客户机的文件名就是传递给端点的文件号。例如,如果我使用http://127.0.0.1:5000/esa/data/0/0
来下载第一个文件,它会下载,但是Chrome/Firefox/IE/Edge提供了将文件名保存为“0”的文件。虽然这没有什么错,但我希望有更好的用户体验。在
如何拦截响应以提供基于请求的URL的文件名?在
这可以通过^{} HTTP头来完成。您可以在这里为新下载的文件指定一个文件名。在
可将其添加到烧瓶中,如下所示:
注意:为了简单起见,
Content-Type
被移到headers
中相关问题 更多 >
编程相关推荐