Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用make_response在Flask中下载文本文件?_Python_Rest_Flask - Fatal编程技术网

Python 如何使用make_response在Flask中下载文本文件?

Python 如何使用make_response在Flask中下载文本文件?,python,rest,flask,Python,Rest,Flask,我想在我的目录中写下一个文本文件,以便在Flask中使用make_response,但我写下的代码不起作用 下面是我用make_response下载文本文件的代码 @api.route('/structure_voice_log', methods=['POST']) def download(): response = make_response("hogehoge") response.headers["Content-Disposition"] = "attachment;

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

下面是我用make_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”,而不是一个文本文件。 我怎样才能做到? 使用此框架可能会出错。
如果您能帮助我,我将不胜感激。

make_response
在第一个参数中输入一个要显示的字符串。 可以改用
send_file
功能()

希望能有帮助。 致意