python - Creating download link for user generated csv file on App Engine -
i having issue when trying create download link (1) generate csv file in-situ , (2) commence download automatically without being redirected page (i.e. ajax request).
what happens follows: not seeing javascript errors , handler csvdownload executes without issues. however, download never starts. reason, if enter url (..website../csvdownload) create file using same handler. idea why is?
any appreciated!
html
<button id="mainreportdownload" type="button"> download report </button>
ajax request
$('#mainreportdownload').on('click', function() { $.ajax({ type: 'get', url: "/csvdownload", beforesend: function() { notification('creating report..','info',false) } }); });
python
def list2csv(data): csv = "" row in data: item in row: csv = csv + item + ',' csv = csv[:-1] + '\n' csv = csv[:-1] return csv class csvdownload(webapp2.requesthandler): def get(self): conn = rdbms.connect(instance=_instance_name, database='test') cursor = conn.cursor() cursor.execute('select email test limit 100') testvalues = [[item item in row] row in cursor.fetchall()] csv = list2csv(testvalues) self.response.headers['content-type'] = 'text/csv' self.response.headers['content-disposition'] = 'attachment; filename=report.csv' self.response.out.write(csv) conn.close()
you have contradictory requirements. normal download happens when browser navigates url sends 'attachment' content-disposition. you're trying use ajax, doesn't navigate no download done. ajax happily reading content, throwing away because don't define success handler telling it. and, of course, if did define handler, there's no way in javascript save file user's computer, obvious security reasons.
Comments
Post a Comment