I am trying to download large pdf file from server and server takes some time to generate pdf and respond so request showing as pending.
I need to show spinner when request starts and hide it when request is completed.
How can I accomplish this using JavaScript ASP.NET MVC.
---UPDATE------
Example controller looks like this:
public ActionResult DownloadFile()
{
return File(@"C:\Temp2\HolidayInnReceipt.pdf", "application/pdf", "Report.pdf");
}
---UPDATE------
Here is sample project.
<style>
#overlay {
position: fixed;
display: none;
width: 100%;
height: 100%;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0,0,0,0.5);
z-index: 99;
cursor: pointer;
}</style>
<button class="btn btn-default btn-lg" onclick="ConfirmDialog('Are you sure you want To Download?')">Download</button>
<br /><br />
<div id="overlay">
<div id="text">
<img src="~/assets/images/loadingimage.gif" style="width:5%;" /><span> Downloading....</span>
</div>
</div>
<label class="error-msg" style="color:green;" itemid="lblmsg"></label>
<script type="text/javascript">
function ConfirmDialog(message) {
debugger;
var x = window.confirm(message)
debugger;
if (x) {
on();
$.ajax({
url: '@Url.Action("YourMVC_Method", "Controller")',
type: 'GET',
contentType: 'application/json;charset=utf-8',
success: function (result) {
debugger;
$("label[itemid='lblmsg']").show();
$('label[itemid="lblmsg"]').text('DownloadSuccess');
off();
},
error: function (ex) {
//alert(ex);
$('label[itemid="lblmsg"]').hide();
off();
}
});
}
else {
$('label[itemid="lblmsg"]').hide();
off();
}
}
function on() {
document.getElementById("overlay").style.display = "block";
}
function off() {
document.getElementById("overlay").style.display = "none";
}
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With