C# How to download large files?

Posted on 2009-05-06
Last Modified: 2013-12-17
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

Question by:F0x88
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3

Author Comment

ID: 24314553
no it doesn't help me
LVL 13

Expert Comment

ID: 24314613
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.


Author Comment

ID: 24314876
still it's not it problem is with buffer is so big but with my own set buffersize it breaks
LVL 16

Expert Comment

ID: 24315472

Author Comment

ID: 24316676
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
LVL 13

Accepted Solution

copyPasteGhost earned 500 total points
ID: 24316703

Author Comment

ID: 24317060
I found why my app broke with custom buffer, I changed not this parametr what i should
thx for help

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

623 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