I have an application in turbogears that dynamically generates an excel spreadsheet, meaning it is not saved as a file in the server, I set the content type and other headers, so it works fine, my question is if it is possible to trigger a download using jquery. Here is what it looks like:
def get_xl(self, **search):
response.headers['Content-type'] = "application/ms-excel"
response.headers['Content-Disposition'] = "attachment;filename=myfile.xls"
book = Workbook()
... code that puts stuff in rows ...
return book.biff_data()
This returns the data so when I go to the url on my browser, it triggers the download, however, I need to post some data,and it would be nice if everything can be handled with jquery. This is what I have so far on jquery side, I know is all wrong though:
var fields = $('#search-form').serializeObject();
if ( !fields.length ) {
alert("Dude, you must search for something.");
return false;
}
$.get('/get_xl',fields.data);
Instead of using jQuery for this, try changing the
get_xlmethod to accept query string parameters, and then just setting the URL to/get_xl?params_hereusing plain ol’ javascript – this will trigger the Save / Run download dialog in the browser.e.g.