Flask:客户端如何下载创建的JSON文件

时间:2014-10-19 21:10:09

标签: json flask

我有一个JSON字符串,我正在从Web表单中读取,我想创建一个临时文件,并允许将文件下载到本地客户端计算机。换句话说,我的app.route读取字符串,将字符串写入文件,然后将文件发送到客户端:

@app.route('/sendFile', methods=['POST'])
def sendFile():
    content = str(request.form['jsonval'])
    with open('zones.geojson', 'w') as f:
        f.write(content)
    return send_file(f)

这项工作的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

this回答所有需要的是指定正确的Response标头:

@app.route('/sendFile', methods=['POST'])
def sendFile():
    content = str(request.form['jsonval'])
    return Response(content, 
            mimetype='application/json',
            headers={'Content-Disposition':'attachment;filename=zones.geojson'})