0

I am running Flask on GAE. I have an issue serving up my file. Everything seems right but nothing pops up in my browser to prompt me to save it and there are no errors in the log console:

@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

UPDATE

I am thinking the problem might be on the javascript side, here is what I currently have and it does not prompt download:

$.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');

I feel like the solution is here but I can't quite figure it out.

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.

Thanks for the help.

Community
  • 1
  • 1
AlexIIP
  • 2,461
  • 5
  • 29
  • 44
  • What shows up in the headers in the response in your browser? – korylprince Oct 27 '13 at 03:04
  • 'HTTP/1.1 200 OK content-type: text/html; charset=utf-8 content-disposition: attachment; filename=exportChecklists.xml Cache-Control: no-cache Expires: Fri, 01 Jan 1990 00:00:00 GMT Content-Length: 481 Server: Development/2.0 Date: Sun, 27 Oct 2013 05:58:03 GMT' – AlexIIP Oct 27 '13 at 05:59
  • Also, r.text is xml in string form, am I creating the response wrong? – AlexIIP Oct 27 '13 at 06:01
  • I also get this, `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` if I click on it in debug console it prompts download...but thats the only way I was able to do it. – AlexIIP Oct 27 '13 at 07:35
  • You have to set window.location to the download URL. JavaScript can't save files to your computer. – korylprince Oct 27 '13 at 13:12
  • Well, I am getting the right headers and data back, I am trying to figure out how to have the browser prompt the user for download. Like the bar in Chrome which says "Save" or "Discard". By default its not doing that. – AlexIIP Oct 27 '13 at 18:51
  • A browser will not save a file requested with ajax. An iframe is one way to accomplish what you want. – korylprince Oct 28 '13 at 01:29

1 Answers1

0

Here is what I did to solve the problem, this may not be the proper way but here is one:

On the Javascript side:

$.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:

  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.

Please correct me if there is a better solution.

AlexIIP
  • 2,461
  • 5
  • 29
  • 44
  • 1
    Well, you're basically downloading the file twice. Once when using ajax to get the header, the second time when the browser actually saves the file. I suggest using two separate views: one that returns the redirect link, and the other that returns the actual file. See http://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax for more info on ajax downloads. – korylprince Oct 28 '13 at 01:28
  • Thanks. What do you mean by two views? (Sorry I am not very proficient in web development) – AlexIIP Oct 29 '13 at 07:14