我想将文件作为附件流式传输到响应中。我有这个功能:
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时如何将文件呈现为附件的任何想法。我正在使用web2py_component,因为我想基于具有表作为选项的选择列表有条件地将输入表单加载到该div目标(div_form_query)中。 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}}
谢谢, MAVE
答案 0 :(得分: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);
});
另一种选择是只调用上面的web2py_component()
,然后在组件HTML中包含执行以下操作的脚本:
window.open('{{=URL('reports', 'form_query')}}' + '/' + choice);