Hello!
I want to stream a file as an attachment in the response. I have this
function:
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)
When I call this controller normally (if I put the streaming code inside
index() for e.g.) it responds by opening up a download popup to save the
file to disk. But when I have this called as a target function from
web2py_component in index.html (to fill a div with the response)like this:
web2py_component("{{=URL('reports', 'form_query.load')}}" + "/" +
jQuery(this).val(), target='div_form_query');
It renders the file inside the DIV 'div_form_query' rather than popup a
download window.
Any ideas how to render the file as an attachment while using
web2py_component. I'm using web2py_component as I want to conditionally
load input forms into that div target (div_form_query) based on a select
list which has tables as options. The index.html looks something like:
{{left_sidebar_enabled,right_sidebar_enabled=True,False}}
{{extend 'layout.html'}}
<h5>{{=message}}</h5>
{{=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>
{{block left_sidebar}}
{{"""=A(T("Administrative Interface"),
_href=URL('admin','default','index'), _class='button',
_style='margin-top: 1em;')"""}}
<!--h6>{{=T("Don't know what to do?")}}</h6-->
<ul>
<li>{{=A(T("Reports"), _href=URL('netman','reports','index'))}}</li>
<li>{{=A(T("Billing"), _href=URL('netman','billing','index'))}}</li>
<li><a href="http://192.168.136.40/zabbix">{{=T('Monitoring')}}</a></li>
</ul>
{{end}}
Thanks,
mave
--