Solved

C# How to download large files?

Posted on 2009-05-06
8
4,150 Views
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)
error:
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?
thx

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();    

            response.Close();    

            // 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                    

                            backgroundWorker1.ReportProgress(iProgressPercentage);

                        }

                        // clean up the file stream                

                        streamLocal.Close();

                    }

                    // close the connection to the remote server            

                    streamRemote.Close();

                }

            }

Open in new window

0
Comment
Question by:F0x88
  • 4
  • 3
8 Comments
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 24314367
0
 
LVL 2

Author Comment

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

Expert Comment

by:copyPasteGhost
ID: 24314613
0
 
LVL 2

Author Comment

by:F0x88
ID: 24314876
still it's not it problem is with buffer is so big but with my own set buffersize it breaks
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 16

Expert Comment

by:CuteBug
ID: 24315472
0
 
LVL 2

Author Comment

by:F0x88
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
0
 
LVL 13

Accepted Solution

by:
copyPasteGhost earned 500 total points
ID: 24316703
0
 
LVL 2

Author Comment

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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

14 Experts available now in Live!

Get 1:1 Help Now