MVC4 - Display upload progress for each file while uploading multiple files

I'm building an mvc 4 application that makes file uploading. When I get HttpPostedFileBase range, I get streams on them and pass to my business logic layer to save them and bind with database records. This is done without any problems. But when displaying current state of uploading progress comes in, I'm a bit confused. I make a second request while an uploading runs, but it waits for first request to be executed completely.

I know that in same browser client instance (actually same session), my requests are synchronized. But there is a solution that I read about, asynchronous actions.

To try asynchronous actions, I used Stream.CopyToAsync(..) instead of Stream.CopyTo(..). I'm also using Task.Delay(10000) to simulate file uploading progress. Then while asynchronous UploadFile action runs, I invoked synchronous UploadProgress on same browser instance. Result is still waiting for first request to complete. Below is the code I use. Where am I wrong?

Here is async action to upload files;

[HttpPost]
public async Task<ActionResult> Upload(PFileUploadModel files)
{
    if (!Session.GetIsLoggedIn())
        return RedirectToAction("Login", "User");

    var fileRequest = Session.CreateRequest<PAddFileRequest, bool>(); //Creates a business logic request
    fileRequest.Files.Add(...);
    var result = await Session.HandleRequestAsync(fileRequest); //Handles and executes a business logic request by checking authority

    if (result)
        return RedirectToAction("List");

    return RedirectToError();
}

And upload progress action is simple as below for now :) :

public ActionResult UploadProgress()
{
    //var saveProgressRequest = Session.CreateRequest<PSaveFileProgressInfoRequest, PSaveFileProgressInfoResponse>();
    //saveProgressRequest.ProgressHandle = progressHandle;
    //var response = Session.HandleRequest(saveProgressRequest);
    return Content("Test!");
}

Thanks for helping.

Answers


async doesn't change the HTTP protocol. The Upload request is in progress until you return the result.

You're probably running into the ASP.NET session lock, which ensures that multiple request handlers don't interfere with each other when reading/writing session state. I believe that MVC always takes a write lock on the session state by default, preventing any other actions from executing simultaneously (in the same session).

You can override this behavior by specifying the SessionState attribute on your controller.


Need Your Help

Bash - why the "0" is not recognized as a number?

bash

I found an interesting Bash script that will test if a variable is numeric/integer. I like it, but I do not understand why the "0" is not recognized as a number? I can not ask the author, hi/shi is...

Is there a feature pack for Visual Studio 2013?

c++ visual-studio

Visual Studio 2008 offered a feature pack with which one could create code-frameworks for office-style applications: