I'm trying to show a downloading gif next to the file link while the browser downloads the file via a POST request. Currently, clicking on the file link does nothing. If I change the .submit(function(){...});
to just .submit();
then it downloads the file but doesn't show the loading gif. If I set the loading gif to .show();
previous to the .submit();
, then it doesn't show until after the file is already downloaded.
Here is the my current structure and JS...
HTML
<a class="file" data-location="directory/file">File.xlsx</a>
<span class="download_loader"></span>
JS
$(document).on('click', 'a.file', function () {
var location = $(this).data("location");
var name = $(this).html();
var loader = $(this).nextAll("span.download_loader");
$('<form action="FileManager/download.php" method="POST" id="temp_download_form"><input type="hidden" name="name" value="'+name+'" /><input type="hidden" name="location" value="'+location+'" /></form>').appendTo('body').submit(function(loader) {
loader.show();
return true;
});
$("#temp_download_form").remove();
});
-- Solution --
I was able to finally get something that is working based on the answer below and the comments. I used a download function from https://github.com/filamentgroup/jQuery-File-Download and also some timeout functions to manage the flow. The final UX is good and allows the download gif to show and hide nicely. See here: http://d.pr/i/KfRP
Final JS Code
// Download Function
function download(url, data, method){
//url and data options required
if( url && data ){
//data can be string of parameters or array/object
data = typeof data == 'string' ? data : $.param(data);
//split params into form inputs
var inputs = '';
$.each(data.split('&'), function(){
var pair = this.split('=');
inputs+='<input type="hidden" name="'+ pair[0] +'" value="'+ pair[1] +'" />';
});
//send request
$('<form action="'+ url +'" method="'+ (method||'post') +'">'+inputs+'</form>')
.appendTo('body').submit().remove();
};
};
// Download File
$(document).on('click', 'a.file', function () {
var location = $(this).data("location");
var name = $(this).html();
var cell = $(this).closest('td');
$('<span class="download_loader"></span>').appendTo(cell);
setTimeout(function(){
download('FileManager/download.php','name='+name+'&location='+location,'post');
}, 1000);
setTimeout(function(){
$("span.download_loader").fadeOut();
}, 2000);
});