javascript - Jquery 1.11.1 - download file and additional callback -
i have form submit django server calling.
$("#my_form").submit();
it server returns xml file executing code:
content = some_data_retrieved_from_database_as_xml() response = httpresponse(content, content_type='text/xml') response['content-disposition'] = 'attachment; ' response['content-disposition'] += 'filename=my_file.xml' response['content-encoding'] = 'utf-8' return response
google chrome downloads file, want register additional callback function, called myfunction(data).
chrome should download file , next call myfunction(this xml file).
tried code, doesn't work:
$("#my_form").bind('ajax:complete', myfunction);
i tried use $.post, after callback function called , unfortunatelly file not downloaded.
so want post
asynchronously, download returned file, , execute callback. 1 possibility "fake" download link:
$.post(post_url, post_data, function(response) { var = document.createelement('a'); a.setattribute('href', 'data:' + response.contenttype + ';charset=' + response.inputencoding + ',' + new xmlserializer().serializetostring(response)); a.setattribute('download', download_filename); a.click(); // file download triggered myfunction(); // additional callback action });
update
as zoran majstorovic mentioned in comment, can try jquery plugin: http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/
just include the source , then
$.filedownload(post_url, { httpmethod: "post", successcallback: myfunction });
for security, need set cookie
set-cookie: filedownload=true; path=/
Comments
Post a Comment