javascript - python flask: How to handle send_file on the client side -
i generated excel file server-side in situ input given on client side using xlwt , stringio. send file client, s/he can save (ideally save dialog).
i trying send_file
, not javascript/jquery/ajax, not know how handle on client side. based on code below (gathered flask homepage), give me hint how there?
thanks help!
here's code:
please note: js code triggered click event on button. works fine when passing json client server , back...
python
import stringio import wordpuzzle # code creates excel file @app.route('/_create_wordsearch') def create_wordsearch(): wordlist = json.loads(request.args.get('wordlist')) # create stringio object output = stringio.stringio() # instantiate puzzlegrid class p = wordpuzzle.puzzlegrid(wordlist) # create puzzle p.main() # create xls file in memory p.output2excel(output) # set start output.seek(0) # send client return send_file(output, mimetype='application/vnd.ms-excel')
js
$(document).ready(function() { $("#create_btn").bind('click', function(){ //get words list var list = []; $("#wordlist option").each(function(){ list.push($(this).val()); }); $.getjson($script_root + '/_create_wordsearch', { wordlist: json.stringify(list) }, function(data){ // goes in here? }); return false; }); });
assuming have div on page use target:
<div id="exportdiv"></div>
replace $.getjson call code this:
var url = $script_root + '/_create_wordsearch'; // hide div, write form $('#exportdiv').hide() .html('<form id="exportform" action="' + url + '" target="_blank" method="get">' + '<textarea name="wordlist">' + json.stringify(list) +'</textarea>' + '</form>'); // submit form $('#exportform').submit();
and on server side, need unescape json. i'm not if can feed markup.unescape() directly json.loads like:
wordlist = json.loads(markup(request.args.get('wordlist')).unescape())
Comments
Post a Comment