I modified
https://groups.google.com/forum/#!msg/web2py/HQU5kD0tGag/LGtjlikzWdsJ
to download csv file without having to first save it to disk:
In View, there is a button 'download' so that when clicked, the view
doesn't change, but a file is downloaded:
<form><input type="hidden" id="PLID" name="probelist" value=""/></form>
{{=A(T('Download'), _type='button', _onclick=\
"jQuery('#PLID').val('%s');ajax('%s',['arg1','arg2'],':eval');
"%( request.vars.my_form_variable),URL('dlCSV') ), _class='btn
btn-primary') }}
And in the controller:
def dlCSV():
:
:
:
rows=db(query).select(selected_fields)
doc64=embed64(data=str(rows),extension='application/csv')
response.headers['Content-Type'] = 'text/csv'
attachment = 'attachment;filename=data.csv'
response.headers['Content-Disposition'] = attachment
return 'window.location="%s";' % doc64
This works great. Just one issue: when I click the "download" button, the
filename is "Unknown". How can I change the filename to "exported.csv" or
something else?
TIA
Ben
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.