Unblock (jQuery BlockUI) after file sent to browser through response stream - asp.net

I have an <asp:button /> that generates a PDF report. I want to use jQuery BlockUI to display a message that says: Generating Report..
Using: Response.BinaryWrite I am then sending the file to the user so it appears as a file download in the browser.
I cannot get $.unblockUI(); to fire. As soon as the file download begins or has completed or is cancelled I want it to dissappear. Currently it never does.. It's as though the page hasn't been re-loaded, I've hit the server but it comes back with the same page.
Things I have tried:
Setting a variable in JS to true so on document.ready() it will call a function if set to true. This doesn't work because even though I change the variable in code it doesn't change the HTML that is sent to the client.
This sort of thing: Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function() { $.unblockUI; }); //register with Microsoft way
$(document).ajaxStop($.unblockUI); //double the insurance and register with jquery way
never gets called..
Can this be achieved with an updatepanel?
Any thoughts?
Also in case it helps:
Response.AddHeader("Content-Disposition", "attachment;filename=""" & ShortFilename & """")
Response.AddHeader("Content-Length", byteArray.Length)
Response.BinaryWrite(byteArray)
Response.Flush()
Response.End()
I can see why this doesn't work sort of, the page is not refreshing in anyway there's just a response stream being sent to the browser, but surely there's an event I can latch on to?

An idea could be to create a child window that does the PDF loading and let the parent figure out when the child window has closed or something.
Is it possible for parent window to notice if child window has been closed?

The solution is to first block the UI as normal. Then make an AJAX request to your report generator, when the PDF is generated store it locally for a short time, or have it in a temporary folder that is cleared out when the user logs out or their login times out. Have the report generator return a success message and a URL.
Have the client ajax request handle the success message, remove BlockUI, then call the URL using:
window.location="http://yourURL/temp/report.pdf
The browser will start to download the file and you are done!
https://stackoverflow.com/a/7660817/181197

Related

Detect when response.redirect has finished?

I have a form that dynamically generates a PDF based on database data. But I don't want to navigate away from the form whilst the PDF is generated and downloaded. So I am using response.redirect to call the .aspx page that generates the PDF and serves it via stream (Have done for many years) so there may be a better option out there now. However I have found people are logging out before the PDF has been sent to the browser which is causing issues.
Is there a way to detect when the reponse.redirect has finished and the file has been downloaded?
I have tried using postmessage and a listener but this doesn't work.
I've also tried setting up and EndRequestHandler as below in my main form:
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler) ;
But this hasn't worked either. The browser is aware as as the tab with the main form has a progress icon in, so there must be a way to intercept the complete event.
Might not be accurate like you want it but I do something sort of similar. When the button is clicked, a "Please Wait..." message appear until the file is ready to be downloaded. The way I do it is the client wait for a cookie. When the server is ready to send the file (after processing) it sets a cookie. Even if the cookie was set in a different request, the client still gets the updated value.

Creating multiple responses from one ASP page

When the user checks a checkbox and clicks a specific button, I already do some stuff on the server and then use Response.Redirect to "reset" the page. I also have a function which allows me to "export" a datatable to an excel spreadsheet by using a application/vnd.openxmlformats-officedocument.spreadsheetml.sheet response.
Both of these work fantastically when separate. But, what I now need to do is have both events take place after one button click.
It seems like I would need to Flush the response after exporting to excel and then redirect the response afterward, but I can't seem to be able to get this to work. I could be way off on this one, and need some assistance. Thanks
EDIT:
Alright, I have decided to go about this by opening a new page (downloadexcel.aspx) which will then initiate the download and close after the download is completed. I am opening downloadexcel.aspx using javascript by writing it to the response of the current page. For some reason, however, the window is not being opened before the original page is re-directed. Is their a method to be called before I redirect? Maybe I have a syntax issue?
Response.Write("<script type='text/javascript'>window.open('~/DownloadExcel.aspx', '', ''); </script>")
Response.Redirect("~/BulkImport.aspx")
If you want to do this in a single HTTP request then you will have a single response. You can either return HTTP 200 (with the file as the content) or HTTP 301 to redirect the client. You can't return both.
The bottom line is that you will need to rethink your approach, you could "clean" the form on the client side using javascript instead of reloading the page using a redirect.

How can I send a file AND the page in an HTTP response?

I have an ASP.NET page where I'm generating an Excel spreadsheet and sending that in the response (content-disposition header). That part works great, but I have a button that never becomes re-enabled because the original page isn't part of the response.
Here's the function I'm using:
Public Shared Sub WriteToResponse(ByVal theWorkBook As Workbook, ByVal FileName As String, ByVal resp As HttpResponse)
Dim theStream As New System.IO.MemoryStream()
theWorkBook.Save(theStream)
Dim byteArr As Byte() = DirectCast(Array.CreateInstance(GetType(Byte), theStream.Length), Byte())
theStream.Position = 0
theStream.Read(byteArr, 0, CInt(theStream.Length))
theStream.Close()
resp.Clear()
resp.AddHeader("content-disposition", "attachment; filename=" & FileName)
resp.BinaryWrite(byteArr)
resp.End()
End Sub
I tried not clearing the response and using AppendHeader instead of AddHeader, but the response still contains only the file.
What am I missing? Do I need to use a different header? I found this unanswered question on the ASP.NET forums; sounds like my problem.
You can't. You can fake the behaviour by opening a popup on clicking the button and have the popup serve up the file and posting back the page from the javascript. But in this manner you are effectively creating 2 requests with 2 responses.
update: if you want to disable the button when they start the download and enable it as soon as the download starts you can't really do this but you could use some trickery to make it appear like that. When they click on the download button use some javascript to disable the button and to create the popup that kicks off the file streaming. When the filestreaming function (WriteToResponse) returns set some value in the users session. In the mean time have the original page poll the server using AJAX to see if the session value is set. If it's set re-enable the button and set to the value to empty or false again (so this works a 2nd time). But this seems like a whole lot of tricks.
but I think in this case the cure is worse then the problem. I assume you want this because generating the file takes a while and you don't want users to reset the flow by pressing the button impatiently while the file is being prepared. To accomplish this I would just show a popup message in the page on button press saying something like "Your download is being prepared. This might take a minute. Thank you for your patience. (click to dismiss this message)"
You cannot send back a file and a new page in the same response.
I'm not sure how the solution fits your particular needs, but remember that you can use Javascript to enable or disable a button right before the file-creating request is sent to the server (so that, by the time spreadsheet download begins for the user, the button is already in the state you want it in).

ASP.NET 2.0 PageComplete Event troubles and File downloading

I am trying to place an action to happen after an entire .aspx page displays. The "action" is a function that uses the Response object to send a file to the user.
More detailed information:
I am trying to replicate the behavior of a link on the page, from a sidebar. I.E. I have a link on the main page for the Export action, and it works fine -- since the page is already displayed before the user clicks it. But when the user is on a sidebar, clicks the link, it should take them back to this main page and then send the file after it displays.
I did some research and thought that using the PageComplete event would be well-suited for this, so I created my event handler and put the call to the export code (it keys off of a query string when loaded from the sidebar) inside my PageComplete event handler. But it behaves just the same way - the browser download box pops up and the page is never loaded before or after.
If it helps to understand what I'm doing here is a snippet of the code used to send the list to the user.
Response.Clear();
Response.BufferOutput = true;
Response.ContentType = "application/ms-excel";
Response.AppendHeader("content-disposition", "attachment;filename=MyList.xls");
Response.Write(listManager.ExportLists(mycode));
Response.End();
I would prefer a way to use a page event to load the page, rather than tinkering with this logic. But, if there is a clean and easier way to get the file sent, and it allows for loading the page then sending the file, that would be fine too.
Is there another Page event I can use besides PageComplete, or is it possible I am missing something?
EDIT: Sorry about the verbosity. I realize that I can't change the way HTTP requests work - I'm only looking for an acceptable solution that achieves more or less the same results. It seems like the way to go is to force a refresh of the page after a couple of seconds (thus ensuring that it loads before the file download code is executed) -- so I am looking for a way to do this as the first answer suggests - refresh to a download. (It doesn't have to be delayed either, if there's a way to refresh with no waiting)
Why doesn't this code work?
private void Page_LoadComplete(object sender, System.EventArgs e)
{
if (Request.QueryString["action"] != null)
{
if (Request.QueryString["action"] == "export")
{
Response.Redirect("MyHome.aspx?action=exportnow", false);
}
if (Request.QueryString["action"] == "exportnow")
{
ExportMasterList();
}
}
}
What it's supposed to do: after page loading is complete, do a Response.Redirect, reloading itself with a different query string. When it reaches the Page LoadComplete event again, the second time it will trigger the function which writes out the file.
What it actually does: Apparently repeats the same problem twice... it goes back to the same problem, how do you execute an action after the page loads, or wait until the page completely finishes loading, then trigger a refresh which will execute the action? Is there no way for ASP.NET to do something by itself without the user clicking on something?
If that's the case, then an auto-refresh after 2 seconds would also be acceptable... but I'm not sure how to do that.
The server can only return one object to the user, a file download or a page. The best you can manage is to return the user to a page that refreshes to a file download.

Showing page load progress with JavaScript

I am retrieving a list of files from a web service and displaying them as links. When someone clicks on the link, the file should start downloading. The problem is that no direct access to the file exists. Here is the process to download a file:
User clicks on link
Server calls web service to retrieve file
Browser is loading for as long as it takes to bring the file back from the web service
Browser finally offers file to user for download
To counter the unfavorable user experience of having to wait for the web service before the download starts, I would like to display a "Loading..." feedback dialog when a link is clicked, and have the dialog go away when the file is ready.
I am using jQuery, and have been playing around with adding an iframe to the page to load the file, but the problem I am having is I don't know how to determine that the file is ready.
I am looking for tips, not only on the client side to give ample feedback, but perhaps also on the server side (using ASP.Net), just in case I can do something to streamline the process. Thank you.
Update: To clarify, this is for downloading a file to a user's machine. What I'm specifically looking for is a way to determine when the file is ready for download. So, when step #4 above takes place, I'd like to remove the "Loading..." message.
It sounds like you need to expose a new request URL on your server, whose only job is to reply whether the web-service-retrieval has completed (or if not, how far it has got).
Have AJAX poll that service every few seconds, and remove the "Loading..." message when it replied that the file is ready.
The client should send the session ID (maybe also a file ID), so that the server knows which file is being asked about. The server side will need to be able to contact the running web-service-retrieval process/thread to ask it how far it got. (If it can't find the thread then presumably it has finished.)
you can use the complete option for $.ajax function in jquery which should get called when the request completes
You can have jQuery to wait for the click of the link and when the link is clicked show the display loading message.
Example that shows how to 'Loading - Please wait' Message Display Script you can follow.
EDIT-
Basically you want a progress bar monitor, which I have never seen it done with just javascript, but with flash or ajax.
Here is a File Upload Demo that should help.
If you're linking to an ASHX that is grabbing the file and writing it back to a stream, you can modify the links click event to display a message.
$(function () {
$("a.someClass").click(function () {
$("div.message").fadeIn(); // show a loading message
return true; // make sure the link still gets followed
});
});
The loading message can just be a spinner or something while waiting for the ASHX to write the response back.
Edit: Since you're not directly linking to the ASHX:
$(function () {
var ashxCallback = function (data, textStatus) {
// data could be: xmlDoc, jsonObj, html, text, etc...
this; // the options for this ajax request
// textStatus can be one of: "timeout", "error", "notmodified", "success", "parsererror"
alert(textStatus);
};
$("a.someClass").click(function () {
$("div.message").fadeIn(); // show a loading message
$.get("some.ashx?id=1234", "", ashxCallback);
return false;
});
});
Using jQuery.get() and jQuery.post().
Edit #2: I just had another thought. What if the Loading... message was in an iframe that redirected to the file? So you would display the iframe with jQuery, it would have the default message of Loading... and a little spinner animation, then set the iframe's src/location to the ASHX that takes forever to load. You could then give it an onload handler from the iframe's parent page that would hide/remove it. Let me know if you need sample code.
If preparation for the file takes less than default timeout of AJAX request, you may start AJAX request, displaying "Loading..." message to user. AJAX call then checks for file's availability, and when it becomes available - returns with the message (success or not) and gives a link for the file. AJAX request can even do file download, but that's for your option.

Resources