window.open('{{=URL('reports', 'form_query')}}' + '/' + jQuery(this).val());

is still the way to trigger the download. The jQuery "change" event handler 
could trigger the component loading *in addition* to triggering the above 
command. Another option might be to include the above JS in a script within 
the component, so when the component loads, the script is run and then 
triggers the download. Either way, I don't think you want to load the file 
itself into the component div, as your code does. If you want HTML content 
displayed in the div, then that's what the component has to return. 
Triggering the download is a separate issue.

Anthony

On Friday, September 7, 2012 8:48:14 PM UTC-4, maverick wrote:
>
> I do want to also display content in the DIV. In addition to displaying 
> content in the DIV, I want the controller to also trigger a file download. 
> A more accurate version of the form_query() controller would be something 
> like:
>
> In reponse to the form submission, it has to return a dict containing a 
> table (Result) that will be loaded to the target DIV as well as trigger a 
> file download. The file is just a static file for now, but eventually it 
> will be a csv export of the query result (rows). This way i display the 
> result as a table and also have the user download the result set as a csv.
>
> def form_query():
> if request.args(0) in db.tables:
> response.generic_patterns = ['load']
> form=SQLFORM(db[request.args(0)])
> if form.validate(keepvalues=True):
> rows = db().select(db.vmt_weekly.ALL)
> Result=SQLTABLE(rows,headers='fieldname:capitalize',truncate=60) 
>  response.headers['Content-Type'] = gluon.contenttype.contenttype('.txt')
> response.headers['Content-Disposition'] = 'attachment; 
> filename=somefile.txt'
>  #NOTE#It has to return: 
> dict(Result=SQLTABLE(rows,headers='fieldname:capitalize',truncate=60),Query=form)
>  
> And also
> trigger the download of somefile.txt. And further somefile.txt will be a 
> csv representation of 'rows' variable above.
>
> return dict(form=form)
> else:
> return dict()
>
> Thanks,
> mave
>
> On Thursday, September 6, 2012 1:20:04 PM UTC-7, Anthony wrote:
>>
>> My response from Stack Overflow:
>>
>> jQuery('#rep_type').change(function(){
>>   window.open('{{=URL('reports', 'form_query')}}' + '/' + 
>> jQuery(this).val());
>> });
>>
>> If you want the file to download as an attachment, don't use a component. 
>> A component creates a div for displaying content, but you just want to 
>> trigger a file download.
>>
>> Anthony
>>
>>
>> On Thursday, September 6, 2012 12:48:56 PM UTC-4, maverick wrote:
>>>
>>> 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
>>>
>>

-- 



Reply via email to