I want to download a file from my web server to my internet page without a real-human clicking or page-load event. It seems rather easy to put these event handlers into the HTML, but I haven't been able to find a solution where jQuery or JavaScript can download a file with a line or two of code, not using a human event.
For more background, I am using AJAX. The user clicks a button, which sets off the AJAX request, which calls a PHP file, which does some work and creates a log file. It can take up to 10 minutes to make the log file so that it is ready to be downloaded. If the AJAX request returns with certain conditions, I want the file to be downloaded to the browser, and if not, then I want it to download nothing. I want it so that the user can kick off the process, go bake a cake, come back, and see that the file has been downloaded.
$.ajax({
type: "POST",
url: "/php/do_work.php",
data:{'foo':bar},
dataType: 'text',
success : function(listen_up) {
if (listen_up == "hide yo kids") {
//download file to browser
} else if (listen_up == "hide yo wife") {
//do something else
}
//the rest of the ajax request...
I want the download to indicate success by putting the filename at the bottom left of the browser window, as seen in the following image:
I am also okay with HTML solutions, as long as they can somehow wait until the AJAX request is done before initiating the download. I am also fine with events, as long as the human-user doesn't have to be the one that sets it off. I would also rather not have to use more server resources than necessary, such as polling, as I would like the server's full attention on creating the log files... but that would an adequate answer if there was no other way.