如何使用make\ u response在Flask中下载文本文件?

2024-05-21 04:47:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在我的目录下写下一个文本文件,以便在Flask中使用make\ u response,但是我写下的代码不起作用

下面是我写的一段代码,用make\ u response下载一个文本文件

@api.route('/structure_voice_log', methods=['POST'])
def download():
    response = make_response("hogehoge")
    response.headers["Content-Disposition"] = "attachment; filename=outbound.txt"
    response.headers["Content-type"] = "text/plain"
    return response

这是我发出的命令

curl -L -i -H "Content-Type: application/json" http://localhost:17354/structure_voice_log -X POST -d '{"voice_log": "hogehoge"}'

以下是上述命令结束后的结果

HTTP/1.0 200 OK
Content-type: text/plain
Content-Length: 8
Content-Disposition: attachment; filename=outbound.txt
Access-Control-Allow-Origin: *
Server: Werkzeug/0.15.5 Python/3.7.5
Date: Tue, 12 Nov 2019 16:15:44 GMT

hogehoge%

我得到的只是一个字符串“hogehoge”,而不是一个文本文件。 我怎样才能做到? 使用这个框架可能会出错。 如果你能帮我,我会非常感激的


Tags: 代码logattachmentmakeresponseoutboundcontentfilename