0

I have an HTML form that calls an ASP.NET page when a button is pressed using JavaScript's window.location. The ASP.NET page runs a process and prompts the user to save the results of the process to a text file on the user's computer.

Because the ouput of the ASP.NET page is a text file, the user never actually navigates away from the HTML form--that is the behavior I want. However, the process involved in generating the text file can take a minute or so. Hence, I'd like my HTML form to display a "Loading" message while the page is running and remove the message when the page is done running.

How can my HTML form know the state of the page it calls? In other words, how can I know when to remove the "Loading" message from the HTML form?

I ended up using a callback with an ASPX WebMethod to solve the issue. Since an ASPX WebMethod cannot save a text file to the browser, I write the file to disk and then return the URL of the file, prompting the user to save it.

2 Answers 2

1

At a high level, you need to make an AJAX call from your JavaScript to some kind of endpoint on the server. Sounds like you're partway there already, but instead of calling the page itself (I'm guessing you're doing a simulated postback from JavaScript), you'd need to call a something that knows how to return a value to your calling JavaScript. An ASP.NET WebMethod is one approach; you could also create a web service. The WebMethod approach would be closer to what you're doing now.

The WebMethod would then return a JSON or XML message containing a success/failure message.

In your JavaScript, then, the flow goes like this:

  • User clicks link
  • Begin some kind of busy animation
  • Send AJAX request to the WebMethod on the server, along with a callback function that will be invoked when the server returns a result
  • WebMethod processes the form
  • Server sends a message back to the browser with a "success!" message
  • Original AJAX function invokes the callback, which would stop the busy animation and display a success message to your user

If you include more details about the technologies you're using, I could be more specific.

Sign up to request clarification or add additional context in comments.

1 Comment

I can see that this would work if I could get my WebMethod to actually prompt the user to save the text file. I call the WebMethod and it does it's processing without any errors but it never prompts the user to save the file. A plain old ASP.NET page does prompt to save the file but I can't get a callback from it to indicate when it's done.
0

Use a XMLHttpRequest

Syntax for creating an XMLHttpRequest object:

xmlhttp=new XMLHttpRequest();

Old versions of Internet Explorer (IE5 and IE6) uses an ActiveX Object:

xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

Then you can use it like this:

   if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      } 
    function getText() {
      xmlhttp.open("GET","url to asp page",true);
      xmlhttp.send(null); 
      xmlhttp.onreadystatechange = fileReady;
      //code for loading bar
    }

   function fileReady() {
        xmlDoc=xmlhttp.responseXML;
       //code for printing or use the file
   }

for detailed info, google :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.