0
我想在响应中将文件作为附件进行流式传输。我有这样的功能:response.stream和web2py_component:无法将文件作为附件流式传输
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.html为EG)它通过打开一个下载弹出保存文件的响应到磁盘。但是,当我有这种被称为从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
如果您希望将文件作为附件下载,那么组件div有什么意义?该div意味着你想在页面上显示一些东西。 – Anthony
安东尼,是的,除了控制器触发下载之外,DIV必须显示控制器的响应。抱歉,如果上面不清楚,我希望控制器(form_query)做两件事:返回一个包含表进入目标DIV并触发文件下载。上面的form_query()代码不会显示返回包含表格的dict的代码。 – maverick
您需要进行两个不同的调用 - 一个用于表格,另一个用于文件(对于那些成为两个单独的函数可能有意义)。查看更新的答案。 – Anthony