Call HttpHandler from javascript
I would use JQuery AJAX and then on success, write a function that will do whatever work you need it to. Plus with AJAX you can show the user an icon that says loading so they know something is actually processing, instead of the page just hanging and nothing appearing to happen.
$.ajax({
url: "FileStorage.ashx",
context: document.body,
success: function(){
// Whatever you want to do here in javascript.
}
});
Ukraine
Updated on June 13, 2022Comments
-
Ukraine about 2 years
I have a simple page with button which calls HttpHandler via JavaScript.
HttpHandler gets lots of files and adds them to a zip file, after finishing work zip file will be added to Response.
This operation can take several minutes. I would like to execute some JavaScript function after finishing work of HttpHandler.
How can I do it?
My code:
<asp:Button ID="btnDownload" runat=server Text="Download" OnClientClick="Download()" /> <script type="text/javascript"> function Download() { var url = 'FileStorage.ashx'; window.open(url); } </script>
UPD 1:
I have found other solution. Using XMLHttpRequest.
Code:
<script type="text/javascript"> var xmlHttpReq = createXMLHttpRequest(); function Download() { var url = 'FileStorage.ashx'; xmlHttpReq.open("GET", url, false); xmlHttpReq.onreadystatechange = onResponse; xmlHttpReq.send(null); } function onResponse() { if (xmlHttpReq.readyState != 4) { return; } var serverResponse = xmlHttpReq.responseText; alert(serverResponse); } function createXMLHttpRequest() { try { return new XMLHttpRequest(); } catch (e) { } try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { } try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } alert("XMLHttpRequest not supported"); return null; } </script>
In onResponse() I can see my zip file in responseText (binary). But I don't have any idea how I can say to browser to download result of working httphandler such as file.
Any ideas?
-
Ukraine about 13 yearsThanks, but I cann't use JQuery, I need other solution
-
Dietpixel about 13 yearsNot sure I can help then. You'll obviously need to know when the handler is finished executing by maybe polling on a timer for the zip file in a directory. Once it's done and finds the file, run a function. Although that seems wrong. Sorry.