简体   繁体   English

python flask:如何在客户端处理send_file

[英]python flask: How to handle send_file on the client side

I generated an excel file server-side in situ from input given on the client side using xlwt and StringIO. 我使用xlwt和StringIO从客户端给出的输入就地生成了一个excel文件服务器端。 Now I would like to send the file back to the client, so that s/he can save it (Ideally with a Save Dialog). 现在,我想将文件发送回客户端,以便他/他可以保存文件(理想情况下使用“保存”对话框)。

I am trying to do that with send_file , but as I am not very good with javascript/jquery/ajax, I do not actually know how to handle this on the client side. 我正在尝试使用send_file做到这一点,但是由于我对javascript / jquery / ajax不太满意,因此我实际上不知道如何在客户端进行处理。 Based on the code below (gathered mostly from the flask homepage), could you give me a hint how to get there? 根据下面的代码(主要来自烧瓶的主页),您能否给我提示如何到达那里?

Thanks for your help! 谢谢你的帮助!

Here's the code: 这是代码:

Please Note: The JS Code is triggered by a click event on a button. 请注意:JS代码是由按钮上的click事件触发的。 It works fine when passing json from client to server and back... 当将json从客户端传递到服务器并返回时,效果很好...

Python 蟒蛇

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')

JS JS

$(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;
    });
});

Assuming you have a div on the page to use as a target: 假设页面上有一个div用作目标:

<div id="exportdiv"></div>

Replace your $.getJSON call with code like this: 用以下代码替换$ .getJSON调用:

var url = $SCRIPT_ROOT + '/_create_wordsearch';

// hide the div, write the form
$('#exportdiv').hide()
    .html('<form id="exportform" action="' + url + '" target="_blank" method="get">'
        + '<textarea name="wordlist">' + JSON.stringify(list) +'</textarea>'
        + '</form>');

// submit the form
$('#exportform').submit();

And on the server side, you will need to unescape the JSON. 在服务器端,您将需要取消转义JSON。 I'm not certain if you can feed Markup.unescape() directly into json.loads but it will be something like: 我不确定是否可以将Markup.unescape()直接输入到json.loads中,但是它将类似于:

wordlist = json.loads(Markup(request.args.get('wordlist')).unescape())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM