3

I have a delegate method with is called periodic while WritingAnObject uploading the file. I would like to update div (ProgressUpdate) in my MVC page with args.PercentDone value. I appreciate any idea? Thanks,

//delegate method

private void displayProgress(object sender, ProgressArgs args)
{
            //Console.WriteLine(args.PercentDone); //I want to display args.PercentDone in the page
}

//Controller

[HttpPost]
public ActionResult WritingAnObject(MyViewModel bovModel)
{
    //DoSomeStuff which is cause calling displayProgress

    return RedirectToAction("ListingSomeInfo", "testpart");
}

//View

<%using (Html.BeginForm("WritingAnObject", "testpart", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {%>   

    <%:Html.TextBox("catname") %>
    <input type="file" id="fileupload" name="fileupload" />
    <input type="submit" value="Upload" />
    <%} %>


<div id= “ProgressUpdate”</div>
tereško
  • 58,060
  • 25
  • 98
  • 150
  • What you're looking for is an asynchronous file upload, and generally speaking that requires `HTML5`, or `Flash`. Search *"HTML upload progress"* here and the first result you'll find is this very highly rated thread discussing the HTML5 option - http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery/8758614 – Snixtor May 07 '13 at 21:00

1 Answers1

0

Here is one approach you could take to display progress back to a user while an operaton on the server is completing. (requires javascript)

1) Write an action that starts the process on the server. This method should update a progress value in session state (so that it is specific to each session the user is running).

2) Write an action that the client can call to return progress. This would read the value in session state. Generally this action will return either a small HTML fragment containing the progress bar filled in to the right amount, or a JSON object containing the progress value.

3) From your client, make a jQuery.ajax() call to asynchronously poll the server for progress while the operation is running and update the UI.

Additional bells&whistles: - an Action to cancel a long running operation - running the task outside the web application (Azure has some excellent features regarding running tasks asynchronously from a web app) - Have the action that returns progress also let the client know if the operation is completed or canceled.

Geoff Cox
  • 6,102
  • 2
  • 27
  • 30
  • These are suitable options for many long-running asynchronous operations, but the HTML4 file control is *not* asynchronous. Async HTMl file uploads need something a little extra. – Snixtor May 07 '13 at 20:59