0

応答の添付ファイルとしてファイルをストリーミングしたい。私はこの機能を持っています:

def form_query():
    response.flash = str(request.args(0))
    response.generic_patterns = ['load']
    response.headers['Content-Type'] = gluon.contenttype.contenttype('.txt')
    response.headers['Content-Disposition'] = 'attachment; filename=somefile.txt'
    #more code goes in here to process request.args here. 
    #Ultimately, the controller is expected to return a dict containing a table and the file to be streamed as an attachment. For now just trying to get the file streamed.
    return response.stream(open('somefile.txt'),chunk_size=1024)

このコントローラーを通常どおり呼び出すと (たとえば、ストリーミング コードを index() 内に配置して index.html に移動すると)、ダウンロード ポップアップが開き、ファイルがディスクに保存されます。しかし、これを index.html の web2py_component からターゲット関数として呼び出すと (応答で div を埋めるため)、次のようになります。

web2py_component("{{=URL('reports', 'form_query.load')}}" + "/" + jQuery(this).val(), target='div_form_query');

ダウンロード ウィンドウをポップアップ表示するのではなく、DIV 'div_form_query' 内のファイルをレンダリングします。

web2py_component の使用中にファイルを添付ファイルとしてレンダリングする方法についてのアイデア。オプションとしてテーブルを持つ選択リストに基づいて、条件付きでその div ターゲット (div_form_query) に入力フォームをロードしたいので、web2py_component を使用しています。index.html は次のようになります。

{{extend 'layout.html'}}

{{=SELECT('Select a report', 
*[OPTION(repts[i].descr, _value=str(repts[i].report)) for i in range(len(repts))], _id="rep_type")}}

<div id="div_form_query"></div>

<script>
    jQuery(document).ready(function(){
    jQuery('#rep_type').change(function(){
     web2py_component("{{=URL('reports', 'form_query.load')}}" + "/" + jQuery(this).val(), target='div_form_query');
    });

    });
</script>
{{end}}

ありがとう、メイヴ

4

1 に答える 1

1

多分これはあなたが望むことをするでしょう:

jQuery('#rep_type').change(function(){
  var choice = jQuery(this).val();
  web2py_component('{{=URL('reports', 'create_table')}}' + '/' + choice,
    target='div_form_query');
  window.open('{{=URL('reports', 'form_query')}}' + '/' + choice);
});

もう 1 つのオプションは、上記の呼び出しのみをweb2py_component()行い、コンポーネント HTML に次のことを行うスクリプトを含めることです。

window.open('{{=URL('reports', 'form_query')}}' + '/' + choice);
于 2012-09-06T18:58:30.633 に答える