Google App Engine and Flask: Serving Files



我在 GAE 上运行 Flask。我在提供文件时遇到问题。一切似乎都正确,但我的浏览器中没有任何弹出提示我保存它,并且日志控制台中没有错误:

@app.route("/submit", methods=["GET"])
def submitChecklist():
... generate json
headers = {'content-type': 'application/json', 'charset':'UTF-8'}
r = requests.post(url, data=json.dumps(jsonstring), headers=headers, stream=True)
print 'payload: ' + r.text
response = make_response(r.text)
response.headers["Content-Disposition"] = "attachment; filename=exportChecklists.xml"
return response

更新

我认为问题可能出在javascript方面,这是我目前拥有的,它不会提示下载:

$.get('submit',
        dat, 
        function(data) {
            if (data.success==1)
                console.log("done")
            else
                alert("There is an exception on server side while submitting the response!")
            },'text');

我觉得解决方案就在这里,但我无法完全弄清楚。

更新 #2

我仍然不知道如何做到这一点,所以我只提供一个文件。虽然下面的解释总体上很好,但我无法弄清楚如何使用 jQuery 只提供 1 个文件。有人可以提供一个如何做到这一点的例子吗?

感谢您的帮助。

这是我为解决问题所做的,这可能不是正确的方法,但这里有一个:

在Javascript方面:

$.get('submit',
        dat,
        function(data, status, request) {
            if (status = 'success'){
                console.log(status);
               $("body").append("<iframe src='" + request.getResponseHeader('redirect')+ "' style='display: none;' ></iframe>");
            }
            else 
                alert("There is an exception on server side while submitting the response!");
        }, 
        'xml'); 

在前端 Python 上:

  headers = {'content-type': 'application/json', 'charset':'UTF-8'}
  r = requests.post(url, data=json.dumps(jsonstring), headers=headers)
  response = make_response(r.text)
  response.headers["Content-Type"] = 'application/xml'
  response.headers.add("redirect", request.url)
  response.headers["Content-Disposition"] = 'attachment; filename="exportChecklists.xml"'
  return response
基本上,我

添加了一个重定向 url,这是我的请求 url,所以当文件准备好时,我只是创建了一个隐藏的 iFrame,现代浏览器重定向到并提示下载。

如果有更好的解决方案,请纠正我。