Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.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
Jquery 谷歌应用引擎和烧瓶:提供文件_Jquery_Python_Google App Engine_Flask_Response - Fatal编程技术网

Jquery 谷歌应用引擎和烧瓶:提供文件

Jquery 谷歌应用引擎和烧瓶:提供文件,jquery,python,google-app-engine,flask,response,Jquery,Python,Google App Engine,Flask,Response,我在用GAE做烧瓶。我在提交文件时遇到了一个问题。看起来一切正常,但我的浏览器中没有弹出提示我保存它的内容,日志控制台中也没有错误: @app.route("/submit", methods=["GET"]) def submitChecklist(): ... generate json headers = {'content-type': 'application/json', 'charset':'UTF-8'} r = requests.post(url, data=json.du

我在用GAE做烧瓶。我在提交文件时遇到了一个问题。看起来一切正常,但我的浏览器中没有弹出提示我保存它的内容,日志控制台中也没有错误:

@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只提供一个文件。有人能举个例子说明如何做到这一点吗


谢谢你的帮助。

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

在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'); 
基本上,我添加了一个重定向url,这是我的请求url,所以当文件准备好后,我只创建了一个隐藏的iFrame,现代浏览器重定向到它并提示下载


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

您浏览器中的响应标题中显示了什么?'HTTP/1.1 200 OK内容类型:text/html;charset=utf-8内容配置:附件;filename=exportChecklists.xml缓存控制:无缓存过期:Fri,1990年1月1日00:00:00 GMT内容长度:481服务器:开发/2.0日期:Sun,2013年10月27日05:58:03 GMT’另外,r.text是字符串形式的xml,我是否创建了错误的响应?我也得到了这个,
http://localhost:8080/submit?model=123&Procedures%5B0%5D=%7B%22cIndex%22%3A%221%22%2C%22title%22%3A%22Checklist1%22%2C%22proc%22%3A%22123%22%2C%22state%22%3A%22123%22%7D
如果我在调试控制台中单击它,它会提示下载…但这是我唯一能够执行此操作的方法。您必须将window.location设置为下载URL。JavaScript无法将文件保存到您的计算机。您基本上下载了两次文件。一次是使用ajax获取标题,第二次是浏览器实际保存文件。我建议使用两个独立的视图:一个返回重定向链接,另一个返回实际文件。有关ajax下载的更多信息,请参阅。谢谢。你说的两种观点是什么意思?(对不起,我不是很精通web开发)
  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