Q:

Downloading a CSV without first saving it to disk: change downloaded filename in web2py

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:
{{=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?

A:

I never used/tried solution given in link shared by you. But I use controller similar to following, to download content of db table in csv format.
def export_to_csv():
    import gluon.contenttype
    response.headers['Content-Type'] = gluon.contenttype.contenttype('.csv')

    response.headers['Content-disposition'] = 'attachment; filename=data.csv'
    query = (db.dummy_table.id > 0)
    return str(db(query).select())

I referred csv() function from controller/appadmin.py for this.
And I dont think, you need ajax to a download file. Normal link will work, it will not change view or page refresh.
{{=A(T('Download'), _type='button', _href=URL('default', 'export_to_csv'))}}

