Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've implemented an asp.net MVC site where users can download large MP3 files (up to 120 MB) and works fine. But the problem is when we send the download request of the file then downloading starts properly but we can not perform any action on site until the file fully download (not getting any response of any another request). How can I do fast download the file and another request also send the response together downloading files? My files are stored on Microsoft Azure storage blob.

What I have tried:

public async Task<string> DownloadMediaFile(string url)
        {
            int lstidx = url.LastIndexOf('/');
            int charslngth = url.Length - 1;
            string getFileNameWithExtension = url.Substring(lstidx + 1, charslngth - lstidx);           
            string fileName = getFileNameWithExtension;
            //Create a stream for the file
            Stream stream = null;

            //This controls how many bytes to read at a time and send to the client
            int bytesToRead = 10000;

            // Buffer to read bytes in chunk size specified above
            byte[] buffer = new Byte[bytesToRead];

            // The number of bytes read
            try
            {
                //Create a WebRequest to get the file
                HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create(url);

                //Create a response for this request
                HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();

                if (fileReq.ContentLength > 0)
                    fileResp.ContentLength = fileReq.ContentLength;

                //Get the Stream returned from the response
                stream = fileResp.GetResponseStream();

                // prepare the response to the client. resp is the client Response
                var resp = Response;

                //Indicate the type of data being sent
                resp.ContentType = "application/octet-stream";

                //Name the file 
                resp.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
                resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());

                int length;
                do
                {
                    // Verify that the client is connected.
                    if (resp.IsClientConnected)
                    {
                        // Read data into the buffer.
                        length = stream.Read(buffer, 0, bytesToRead);

                        // and write it out to the response's output stream
                        resp.OutputStream.Write(buffer, 0, length);

                        // Flush the data
                        resp.Flush();

                        //Clear the buffer
                        buffer = new Byte[bytesToRead];
                    }
                    else
                    {
                        // cancel the download if client has disconnected
                        length = -1;
                    }
                } while (length > 0); //Repeat until no data is read
            }
            finally
            {                
                if (stream != null)
                {
                    //Close the input stream
                    stream.Close();
                }
            }
            return "success";
        }
Posted
Updated 7-Sep-18 4:36am

This is most likely due to the session state behaviour - by default, it's set to Required, which means only one request can be served at a time.

Since you don't seem to be using session state or TempData in your action, you can disable it. Unfortunately, you need to do that at the controller level, so you'll need to move your DownloadMediaFile action to a different controller. Then add the SessionState attribute to that controller:
C#
[SessionState(SessionStateBehavior.Disabled)]
public class DownloadController : Controller
{
    ...
 
Share this answer
 
Comments
Deepak Tiwari (D'pak) 10-Sep-18 2:26am    
Thanks, it is working fine.
Deepak Tiwari (D'pak) 10-Sep-18 5:47am    
How can I improve my code for fast downloading?
Do your download in a separate thread
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900