So I have a form that allows users to upload a file, the request sends the file, and the response is the file (to download).
However, as the processing of the file can take a few seconds, I want to show a "loading animation" while the file is being processed, but basically on the client side, while the browser awaits a response from the server (with the file to download).
I thought about something like this:
HTML:
<form role="form" action="/process" method="POST" enctype="multipart/form-data" name="fileform" id="fileform">
<div class="col-lg-6">
<label class="btn btn-default">Browse… <input type="file" style="display: none;" accept=".txt,text/plain" name="file" id="file">
</label>
</div>
<div class="col-lg-6">
<button type="submit" class="btn btn-default">Process</button>
</div>
</form>
<div class="animationload" id="loader">
<div class="osahanloading"></div>
</div>
JS
$("#fileform").submit(function(e) {
e.preventDefault();
$('#loader').show();
var formData = new FormData();
formData.append('file', $('input[type=file]')[0].files[0]);
var action = $('#fileform').attr('action');
jQuery.ajax({
type: 'POST',
url: action,
data: formData,
processData: false,
contentType: false,
success: $('#loader').hide()
});
});
but this does not show the loader div, nor prompts the user to download the file...
Any other way I can achieve this?