応答の添付ファイルとしてファイルをストリーミングしたい。私はこの機能を持っています:
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}}
ありがとう、メイヴ