This might appear to be a question for noobs but I want to know exactly what code is required to start a download when a user clicks on the download button. Let's say I have this webpage and a form which contains filters (i.e selecting a date range) which the users sets and when he clicks on the download button a csv document is created on his hard drive. Which module must I communicate to for the download to start, for the file to have this extension and to contain X data. I heard that you must set some fields in the HTTP header but I'm not too sure how I should go about it.
EDIT: The file download now works but my file now contains all the html previously written on the page as well as the data to download. Here's a snippet of my code. I removed a lot of statements to facilitate readability
# Handler class which handles the page /Download
class downloadsHandler(webapp2.RequestHandler):
def get(self):
# write download page on the browser
template = JINJA_ENVIRONMENT.get_template('/pages/download.html')
self.response.write(template.render(template_values))
# data to download
buf = getLatestdata()
# size of data
size = sys.getsizeof(buf)
# set HTTP headers to notify server of download
self.response.headers["Content-Type"] = "text/csv"
self.response.headers["Cache-Control"] = "no-cache, must-revalidate"
self.response.headers["Content-Disposition"] = "attachment; filename=kioskData.csv"
self.response.headers["Content-Length"] = size
self.response.headers["Content-Transfer-Encoding"] = "binary"
# generate download
self.response.write(buf)
How do I tell the browser only to include the data to download?