jquery.ajax file download

瀏覽次數:122 次
跳到第一則未讀訊息

lucas

未讀,
2017年12月13日 凌晨12:16:002017/12/13
收件者:web2py-users
hello one and all,

now i'm really stuck.  and i know this is more of a jQuery question, but a web2py, but you guys know your stuff and i can't find the solution.  ok, here it is.

i have a form with two buttons, view and download.  view drops the data into a div using the web2py.ajax function.  download is supposed to download the data into a file on the harddrive.  the latter works great if i use the FORM _action deal.  however, i decided to put both buttons on the form and use jQuery to do the deal.  so view works fine and download almost works great.  here is my javascript code:

jQuery(function() {
    jQuery('input[type="submit"]').click(function() {
        var cObj = jQuery(this);
        console.log(cObj.prop('id')+' clicked');
        //good code for getJSON inputs
        var o,r = { 'submit': cObj.prop('name') };
        jQuery('input[name="submit"]').val(cObj.prop('name'));
        jQuery('div#body1 input[type="text"], input[type="hidden"], input[type="radio"]:checked').each(function(i, v) { o = jQuery(v); r[o.prop('name')] = o.val(); });
        jQuery('div#body1 input[type="checkbox"]').each(function(i, v) { o = jQuery(v); r[o.prop('name')] = o.is(':checked'); });
        console.log(r);
        //went with ajax method instead
        if (cObj.prop('name') == 'view') {
            //works fine
            jQuery('div#body2').empty();
            ajax('{{=URL(c="dna", f="view")}}', ['page', 'seqi', 'seq_length', 'seqf', 'invert', 'reverse', 'file_structure', 'nucleotides', 'submit'], 'body2');
        }
        else {
            r['submit'] = 'download';
            //almost works fine
            jQuery.ajax({
                url: '{{=URL(c="dna", f="view")}}',
                data: r,
                success: function(rslt) {
                    console.log(rslt);
                }
            });
        }
    });
});

so that last jQuery.ajax code under "//almost works fine" does actually download the proper data and displays it in the console correctly. 

how do i get it to save that result "rslt" to an actual file on the harddrive by reading the below response headers from the server?

here is a snippet of the download part from web2py controller:

    from cStringIO import StringIO
    stream = StringIO()
    if (inputs.file_structure == 'xml'):
        from gluon.serializers import xml
        stream.write(str(XML(xml(dct))))
    elif (inputs.file_structure == 'json'):
        from gluon.serializers import json
        stream.write(json(dct))
    else:
        stream.write(r"%s" % dct)
    response.headers['Content-Type'] = 'application/octet-stream; charset=us-ascii'
    response.headers['Content-disposition'] = 'attachment; filename=hsDNA_%s_%s_%s_IQa.%s' % (dct['locus']['chromosome'], dct['locus']['locus'], dct['locus']['file_type'], dct['inputs']['file_structure'])
    return stream.getvalue()

where you can clearly see the last few response.headers are set to pass the filename and content-type.

thank you in advance, lucas

Val K

未讀,
2017年12月13日 下午3:32:272017/12/13
收件者:web...@googlegroups.com
but why not just :
...
else {
            r
['submit'] = 'download';

            window
.open( '{{=URL(c="dna", f="view")}}')
...

lucas

未讀,
2017年12月13日 晚上7:51:292017/12/13
收件者:web2py-users
thank you for that, I just added:

window.open( '{{=URL(c="dna", f="view")}}?'+jQuery.param(r) )

which includes the parameters required on the server end.  works great and it is simple.  Lucas
回覆所有人
回覆作者
轉寄
0 則新訊息