Python Flask send_file StringIO No files are downloading

时间:2018-06-19 11:12:46

标签: python jquery flask

The Python Code

def export():
    row = ['hello', 'world']
    proxy = StringIO()
    writer = csv.writer(proxy)
    writer.writerow(row)
    mem = io.BytesIO()
    mem.write(proxy.getvalue().encode('utf-8'))
    mem.seek(0)
    return send_file(mem, as_attachment=True, attachment_filename='test.csv',mimetype='text/csv')

JQuery Code

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
    jQuery('button.export').click(function() {
        jQuery.ajax({
            url: '/export',
            data: {'json': JSON.stringify(jQuery(this).data('json'))},
            type: 'POST',
            success: function(rsesponse) {
                console.log(response);
            },
            error: function(error) {
               console.log(error);
            }
        });
    });
});
</script>

I see hello, world in the console and response but no files will be downloaded. In HTML I have an export button, once I click it, CSV file I should be able to download. How can I do it?

0 个答案:

没有答案