I am running Flask on GAE with a google app engine backend. I submit a form on my site, send it to the backend, the backend returns an xml file and I would like to serve up that xml file. However, I have an issue serving up my file.
This is the only way I could figure out how to serve up my file, it works, but it returns 2 identical files in the browser instead of one, what am I doing wrong?
On the frontend jQuery:
$.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');
On the frontend Python:
@app.route("/submit", methods=["GET"] )
def submit():
... do some stuff to get the data and url packaged...
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
Basically, I added a redirect url which was my request url to start with, so when the file was ready, I just created an hidden iFrame that modern browsers redirect to and prompt for download.
How can I modify this to only get 1 file instead of 2?
Thanks for the help.
UPDATE
I have tried doing a synchronous post and still no prompt for download. (I am making a call to my server, and response comes as an XML file from my server).
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open( "POST", "/submit", false );
xmlHttp.setRequestHeader("Content-type", "application/json");
xmlHttp.send( JSON.stringify(dat ));
return xmlHttp.responseText;
Response Headers:
Cache-Control:no-cache
content-disposition:attachment; filename="exportChecklists.xml"
Content-Length:76
content-type:application/xml
Date:Fri, 27 Dec 2013 22:59:27 GMT
Expires:Fri, 01 Jan 1990 00:00:00 GMT
Server:Development/2.0
UPDATE #2
I still can't figure out how to do this so I only serve one file. While the below explanation is good in general, I can't figure out how to serve only 1 file using jQuery. Could someone please provide an example on how to do this.