title: generating a file with django to download with javascript/jQuery
body: <p>Using Django, I want to make some data available for download.</p>
  
  <p>My jQuery call looks like this so far:</p>
  
  <pre class="lang-jquery prettyprint-override"><code>$.getJSON("/get_data", 
              { users: users, study: "{{study.id}}" } ,
              function(json){
                  alert('some data!');
              }
  );
  </code></pre>
  
  <p>This calls one of my Django views, which in turn generates some JSON and attempts to make that JSON text in a file to download</p>
  
  <pre class="lang-python prettyprint-override"><code>jsonResponse = json.dumps(data, cls=DjangoJSONEncoder)
  
  jsonFile = cStringIO.StringIO()
  jsonFile.write(jsonResponse)
  
  response = HttpResponse(jsonFile, mimetype='application/json')
  response['Content-Disposition'] = 'attachment; filename=data.txt'
  
  return response
  </code></pre>
  
  <p>However, this does not work. After looking around for a while, I believe I should change something on both ends – the Javascript and the python/Django code – but I'm unclear what exactly.</p>
  
  <p>For Python, my main concern is in the use of <a href="http://docs.python.org/library/stringio.html#module-cStringIO" rel="nofollow noreferrer">cStringIO</a> (especially the fact that I cannot execute close on jsonFile before returning without prompting a "ValueError: I/O operation on closed file").</p>
  
  <p>Maybe I should also be using a FileWrapper (like in this <a href="https://stackoverflow.com/questions/908258/generating-file-to-download-with-django">post</a>), but I get identical results with and without it.</p>
  
  <p>For Javascript, I'm not sure what should go into my success handler function.</p>
  
  <p>Any pointers would be greatly appreciated!</p>