I'm maintaining a legacy VB.Net Webforms App and I've got a strange issue after adding one section.
This is this code in the aspx page which shows the giphy.gif while doing a postback:
<style type="text/css">
.modalWait
{
position: fixed;
top: 0;
left: 0;
background-color: black;
z-index: 99;
opacity: 0.5;
filter: alpha(opacity=80);
-moz-opacity: 0.8;
min-height: 100%;
width: 100%;
}
.loading
{
font-family: Arial;
font-size: 10pt;
/*border: 5px solid #67CFF5;*/
width: 100px;
height: 100px;
display: none;
position: fixed;
background-color: transparent;
z-index: 999;
}
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
function ShowProgress() {
setTimeout(function () {
var modal = $('<div />');
modal.addClass("modalWait");
$('body').append(modal);
var loading = $(".loading");
loading.show();
var top = Math.max($(window).height() / 2 - loading[0].offsetHeight / 2, 0);
var left = Math.max($(window).width() / 2 - loading[0].offsetWidth / 2, 0);
loading.css({ top: top, left: left });
}, 200);
}
$('form').live("submit", function () {
ShowProgress();
});
</script>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<form runat="server">
<asp:ScriptManager runat="server"></asp:ScriptManager>
<!-- Page Wrapper -->
<div id="wrapper">
<!-- More Code -->
</div>
<!-- End of Page Wrapper -->
<div class="loading" align="center">
<%--Loading. Please wait.<br /><br />--%>
<img src="../img/giphy.gif" />
</div>
</form>
And works perfectly for all the queries which populate the controls - the .gif appears while the database is called and then goes away.
But then I added a function to read the datagrid into a .csv and then download it. It works perfectly, except when I add this section in the code behind:
Dim bytes As Byte() = Encoding.ASCII.GetBytes(sb.ToString())
Response.Clear()
Response.ContentType = "text/csv"
Response.AddHeader("Content-Length", bytes.Length.ToString())
Response.AddHeader("Content-disposition", "attachment; filename=contacts.csv")
Response.Write(sb.ToString())
Response.Flush()
Response.End()
The file downloads perfectly ... but the giphy.gif is still there ... it doesn't go away even though the postback has finished.
What am I doing wrong?
As VDWWD explained ("the UI is never updated because the server can only send one type of response at a time (a file or a html page)") your real problem is that you have no idea when the file download gets completed (or to be exact, when the server-side code, which prepares the file, has finished executing).
There are more than a handful of questions about this here on SO and amazingly almost always the answer is you cannot know!
Well, not quite!
You can always let the client know by sending back a uniquely named cookie containing a time stamp. On the client, the javascript code tries to detect the cookie presence and when it does it hides any loading image (or text or whatever) you've shown.
So, without further ado here's the code (I only used the
loading
css class to make the sample simpler and smaller):Code-behind VB.NET:
Code-behind C#:
I hope this helps. If it does I suggest we edit the title (and maybe some of the content) of the question so as to help others finally find a working solution to a problem that hasn't been really answered all this time.
You are downloading a file (
contacts.csv
) the UI is never updated because the server can only send one type of response at a time (a file or a html page), so the image will remain visible after download.To fix this either open the download in a new browser window.
If the download must come after a PostBack then the above solution will not work. A possible solution would be to just hide the image after the average download time of the file.