Solved

Buffer 40MB or larger files does not work

Posted on 2012-03-19
3
278 Views
Last Modified: 2012-03-21
Hi

I have this code: what it does is it loads a mp3 file into memory, (using jwplayer), with small files (under 7mb) I can skip forward or move back. But when I load a 40mb file it look like it has to load the whole file into memory. So I cant move forward or back (user exp sucks).

Does anyone know how to get this to work?


public static void ResumableFile(string filename, string fullpath, string contentType)
    {
        System.IO.Stream iStream = null;

        // Buffer to read 10K bytes in chunk:
        byte[] buffer = new Byte[10000];

        int length;
        
        try
        {
            FileStream myFile = new FileStream(fullpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            BinaryReader br = new BinaryReader(myFile);
            try
            {
                iStream = new System.IO.FileStream(fullpath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);          
                
                HttpContext.Current.Response.AddHeader("Accept-Ranges", "bytes");
                HttpContext.Current.Response.Buffer = false;

                // Total bytes to read:
                long fileLength = myFile.Length;
                
                long startBytes = 0;

                //int pack = 10240; //10K bytes


                int pack = 1048576; //1024K bytes

                if (HttpContext.Current.Request.Headers["Range"] != null)
                {
                    HttpContext.Current.Response.StatusCode = 206;
                    string[] range = HttpContext.Current.Request.Headers["Range"].Split(new char[] { '=', '-' });
                    startBytes = Convert.ToInt64(range[1]);
                }
                HttpContext.Current.Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
                if (startBytes != 0)
                {
                    HttpContext.Current.Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
                }
                HttpContext.Current.Response.AddHeader("Connection", "Keep-Alive");
                HttpContext.Current.Response.ContentType = contentType;
                //HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));

                br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
                int maxCount = (int)Math.Floor((double)((fileLength - startBytes) / pack)) + 1;

                // Read the bytes.
                while (fileLength > 0)
                {
                    // Verify that the client is connected.
                    if (HttpContext.Current.Response.IsClientConnected)
                    {
                        // Read the data in buffer.
                        length = iStream.Read(buffer, 0, 10000);

                        // Write the data to the current output stream.
                        HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);

                        // Flush the data to the HTML output.
                        HttpContext.Current.Response.Flush();

                        buffer = new Byte[10000];
                        fileLength = fileLength - length;
                    }
                    else
                    {
                        //prevent infinite loop if user disconnects
                        fileLength = -1;
                    }
                }
            }
            catch
            {
                HttpContext.Current.Response.ContentType = "text/html";
                HttpContext.Current.Response.Write("Error : file not found");
            }
            finally
            {
                br.Close();
                myFile.Close();
            }
        }
        catch
        {
            HttpContext.Current.Response.ContentType = "text/html";
            HttpContext.Current.Response.Write("Error : file not found");
        }
    }

Open in new window

0
Comment
Question by:mousemat24
  • 2
3 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 37737339
>>But when I load a 40mb file it look like it has to load the whole file into memory.

Correct - that is what you have coded (read the file in the while loop).

>>Does anyone know how to get this to work?

Yes - don't use the while loop, read the first buffer load then read only when it is required.  (Maybe you should read the file in a thread)
0
 

Author Comment

by:mousemat24
ID: 37737366
AndyAinscow, dont suppose you can send me the code? Many thanks
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 37737582
It is basically what you currently have - just remove the while loop and keep track of how much you have read from the file.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now