我想在响应中将文件作为附件流式传输。我有这个功能:
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}}
谢谢,马夫