ezile
ezile

Reputation: 571

How to make the download action asynchronous in asp.net mvc 3?

I am streaming a file to client for download. But it may happen that file size could be really big (upto few GBs) and thus I don't want to block the user to click other buttons on the webpage which goes to the same controller as Download. From reading on internet, I found that I can make it asynchronous using "Async" and "Completed" suffixes and this is my code:

    public void DownloadAsync(string filename, string Id, string docId)
    {
        AsyncManager.OutstandingOperations.Increment();

        // code to get the file from server and send it to client.

        AsyncManager.OutstandingOperations.Decrement();

    }
    public ActionResult DownloadCompleted()
    {
        return RedirectToAction("Index");
    }

     public string OtherAction()
     {
         // code for this action.
     }

When I click the Download on webpage and also clicks the "OtherAction" button. It still process the requests synchronously. The "OtherAction" just returns a string to user and is not time intensive and that's why I didn't make it asynchronous.

Do I need to include some code between the .Increment() and .Decrement() operations to wrap the code to download file inside "something" to start a new thread or something like that? I am not able to figure out what other piece I am missing here. I am inheriting the controller from AsyncController.

Upvotes: 3

Views: 2020

Answers (1)

Eduardo Molteni
Eduardo Molteni

Reputation: 39413

I think that you are missing some concepts here. You have two parts.

The server
Every request is asynchronous even without using any Async, so the user can send other requests to the server without being blocked.

The client
As long as the user starts the download and don't exit the browser or stop the download, the user can keep doing operations in the same tab or in another. The request that it's being completed by the download don't stop.

So, you don't need to make anything async in the server. I would just recommend my users to use some download manager if the download is several GB heavy

Upvotes: 2

Related Questions