title: python flask: How to handle send_file on the client side
body: <p>I generated an excel file server-side in situ from input given on the client side using xlwt and StringIO. Now I would like to send the file back to the client, so that s/he can save it (Ideally with a Save Dialog).</p>
  
  <p>I am trying to do that with <code>send_file</code>, but as I am not very good with javascript/jquery/ajax, I do not actually know how to handle this on the client side. Based on the code below (gathered mostly from the flask homepage), could you give me a hint how to get there?</p>
  
  <p>Thanks for your help!</p>
  
  <p>Here's the code:</p>
  
  <p>Please Note: The JS Code is triggered by a click event on a button. It works fine when passing json from client to server and back...</p>
  
  <p><strong>Python</strong></p>
  
  <pre><code>import StringIO
  import wordpuzzle # The code that creates the excel file
  
  @app.route('/_create_wordsearch')
  def create_wordsearch():
  
      wordlist = json.loads(request.args.get('wordlist'))
  
      # create a stringIO object
      output = StringIO.StringIO()
  
      # Instantiate the PuzzleGrid class
      p = wordpuzzle.PuzzleGrid(wordlist)
  
      # Create the Puzzle
      p.main()
  
      # Create the xls file in memory
      p.output2excel(output)
  
      # Set back to start
      output.seek(0)
  
      # send back to client
      return send_file(output, mimetype='application/vnd.ms-excel')
  </code></pre>
  
  <p><strong>JS</strong></p>
  
  <pre><code>$(document).ready(function() {
      $("#create_btn").bind('click', function(){
          //Get all words from list
          var list = [];
          $("#wordlist option").each(function(){
              list.push($(this).val());
          });
          $.getJSON($SCRIPT_ROOT + '/_create_wordsearch', {
              wordlist: JSON.stringify(list)
          }, function(data){
              // What goes in here?
          });
          return false;
      });
  });
  </code></pre>