I have a page with a download button on it. When A user presses the download button (which is just a link), a excel file is generated and returned as download. Generating this file could take up to a minute.
Currently the user only sees that something is happening because his browser is loading. I would like to make it a bit more visible that something is happening. For that, I have a div to show a spinner:
#contentLoading {
position: fixed;
z-index: 100000;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.loading {
position: absolute;
top: 50%;
left: 50%;
display: inline-block;
border-width: 30px;
border-radius: 20%;
-webkit-animation: spin 3s linear infinite;
-moz-animation: spin 3s linear infinite;
-o-animation: spin 3s linear infinite;
animation: spin 3s linear infinite;
}
.style-3 {
border-style: double;
border-color: #1C90F3 #ff0000;
}
@keyframes spin {
100% {
transform: rotate(359deg);
}
}
<div id="contentLoading">
<span class="loading style-3"></span>
</div>
I'm now looking for a way to display this spinner while the download is created. I can trigger it with javascript like this:
$("#contentLoading").show()
and hide it like this:
$("#contentLoading").hide()
But I don't know what to do in between them. I tried a Ajax call:
$('#exportExcel').click(function () {
$("#contentLoading").show()
$.ajax({
url: 'download_excel_excel.php',
type: 'POST',
success: function () {
$("#contentLoading").hide()
}
})
});
But with that, no download is returned to the browser
I also tried this:
$('#exportExcel').click(function () {
$("#contentLoading").show()
window.location = '/progs/q-store/projecten_ea/projectenlijst_excel.php';
$("#contentLoading").hide()
});
But with that, the spinner is not shown.
Any idea how I can accomplish this?
JSONobject to the client to inform him that file has been downloaded successfully.$('#exportExcel').click(function () { $("#contentLoading").show() });should be enough to show spinner. To hide it it's a bit trickier. You would want to change you approach and use GET request that would generate proper content as well as content-disposition headers. Then you would indeed make GET request and do what you are doing in the first snippet.