• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5095
  • Last Modified:

C# How to download large files?

I wrote file downloader but it doesn't work with bigger files like 100mb...
have problem in this line:
iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)
Unable to read data from the transport connection.. no space for buffer etc

when i trying to make this buffer smaller
byte[] byteBuffer = new byte[655336];
it doesn't works too ;/
what I should change in my code to download bigger files?
have you know any good examples to download large files using httpwebresponse?

best regards
// the URL to download the file from
            string sUrlToReadFileFrom = _dl;    
            // the path to write the file to
            Uri _uri = new Uri(_dl);
            string sFilePathToWriteFileTo = _path+Path.GetFileName(_uri.AbsolutePath);
            // first, we need to get the exact size (in bytes) of the file we are downloading    
            Uri url = new Uri(sUrlToReadFileFrom);    
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);    
            System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();    
            // gets the size of the file in bytes    
            Int64 iSize = response.ContentLength;
            // keeps track of the total bytes downloaded so we can update the progress bar    
            Int64 iRunningByteTotal = 0;
            // use the webclient object to download the file    
            using (System.Net.WebClient client = new System.Net.WebClient())
                // open the file at the remote URL for reading        
                using (System.IO.Stream streamRemote = client.OpenRead(new Uri(sUrlToReadFileFrom)))
                    // using the FileStream object, we can write the downloaded bytes to the file system            
                    using (Stream streamLocal = new FileStream(sFilePathToWriteFileTo, FileMode.Create, FileAccess.Write, FileShare.None))
                        // loop the stream and get the file into the byte buffer                
                        int iByteSize = 0;                
                        byte[] byteBuffer = new byte[iSize];
                        while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
                            // write the bytes to the file system at the file path specified                    
                            streamLocal.Write(byteBuffer, 0, iByteSize);                    
                            iRunningByteTotal += iByteSize;                     
                            // calculate the progress out of a base "100"                    
                            double dIndex = (double)(iRunningByteTotal);                    
                            double dTotal = (double)byteBuffer.Length;                    
                            double dProgressPercentage = (dIndex / dTotal);                    
                            int iProgressPercentage = (int)(dProgressPercentage * 100);                     
                            // update the progress bar                    
                        // clean up the file stream                
                    // close the connection to the remote server            

Open in new window

  • 4
  • 3
1 Solution
F0x88Author Commented:
no it doesn't help me
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

F0x88Author Commented:
still it's not it problem is with buffer is so big but with my own set buffersize it breaks
F0x88Author Commented:
i know this one is so big project i need simple solution my code needs few small changes to make buffer for example 1024 and download all file, it can't be that hard
F0x88Author Commented:
I found why my app broke with custom buffer, I changed not this parametr what i should
thx for help
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now