Click here to Skip to main content
15,443,630 members
Articles / Web Development / ASP.NET
Posted 29 Dec 2011


57 bookmarked

Resume Support for File Downloads

Rate me:
Please Sign up or sign in to vote.
4.80/5 (34 votes)
9 Jul 2012CPOL1 min read
Partial downloading of files.

As we all know, downloading a file from a website is done by the click on a link, but if we want to develop an application which downloads a list of files for us, we use a web request to download files. But due to network interruptions, downloading a file is a problem when the size of the file is large. Here we will learn how to download a file partially so that the we can download a big file easily in parts.

This process uses the HttpWebRequest and HttpWebResponse classes of .NET.

The below code uses the FileStream, HttpWebRequest, and HttpWebResponse classes and their methods. Before we create a request for the file we want to download, we should know if the file which we are going to download has already been downloaded previously or if this is the first request. If already downloaded, then we create an object of the FileStream class with Append mode otherwise we create an object of FileScream with Create mode. After that we are required to know how much content has been downloaded already. For this, we use the FileInfo class from which we check the length of the downloaded content. The most important point is that in partial downloading, the length is required to add to our HttpWebRequest by the method HttpWebRequest.AddRange(length) which downloads content after the existing length…

static void DownloadFile(string sSourceURL, string sDestinationPath)
    long iFileSize = 0;
    int iBufferSize = 1024;
    iBufferSize *= 1000;
    long iExistLen = 0;
    System.IO.FileStream saveFileStream;
    if (System.IO.File.Exists(sDestinationPath))
        System.IO.FileInfo fINfo = 
           new System.IO.FileInfo(sDestinationPath);
        iExistLen = fINfo.Length;
    if (iExistLen > 0)
        saveFileStream = new System.IO.FileStream(sDestinationPath, 
          System.IO.FileMode.Append, System.IO.FileAccess.Write, 
        saveFileStream = new System.IO.FileStream(sDestinationPath, 
          System.IO.FileMode.Create, System.IO.FileAccess.Write, 
    System.Net.HttpWebRequest hwRq;
    System.Net.HttpWebResponse hwRes;
    hwRq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(sSourceURL);
    System.IO.Stream smRespStream;
    hwRes = (System.Net.HttpWebResponse)hwRq.GetResponse();
    smRespStream = hwRes.GetResponseStream();
    iFileSize = hwRes.ContentLength;
    int iByteSize;
    byte[] downBuffer = new byte[iBufferSize];
    while ((iByteSize = smRespStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
        saveFileStream.Write(downBuffer, 0, iByteSize);


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

Written By
Software Developer (Senior) Super Eminent Softwares
India India
My name is Peeyush Shah and I am working with Microsoft.Net Framework at Jaipur, India with a Taxaction Software Development Company Named Professional Softec Pvt. Ltd.

My main focus is the development of online application.
We also develop custom software solutions for our customers and are able to support developers in using the following technologies, because we use them every day

- .NET Framework 2.0
- C#
- SQL Server 2005
- XML Webservices
- Payment systems

Feel free to take a look at the website and see Microsoft's new technologies in action
This is a Organisation

43 members

Comments and Discussions

GeneralSuperb Pin
ganesh165-Apr-18 22:14
Memberganesh165-Apr-18 22:14 
PraiseGood article, very helpful Pin
Member 1144868119-Sep-17 11:49
MemberMember 1144868119-Sep-17 11:49 
Good article, very helpful
Suggestionvery good, some modifications though... Pin
Omar Gameel Salem25-Dec-13 3:00
professionalOmar Gameel Salem25-Dec-13 3:00 
GeneralConversion to VB Pin
sounddude00730-Oct-12 4:02
Membersounddude00730-Oct-12 4:02 
GeneralRe: Conversion to VB Pin
spiritdead5-Jul-13 13:33
Memberspiritdead5-Jul-13 13:33 
QuestionResume download, file size larger than the orginal Pin
Member 433659412-Oct-12 6:17
MemberMember 433659412-Oct-12 6:17 
GeneralMy vote of 5 Pin
Sunil P V15-Aug-12 23:58
MemberSunil P V15-Aug-12 23:58 
QuestionExcellent! Pin
Yawar Abbas31-Jul-12 13:23
MemberYawar Abbas31-Jul-12 13:23 
AnswerRe: Excellent! Pin
Technoses16-Jul-15 19:12
MemberTechnoses16-Jul-15 19:12 
QuestionVote of 4 Pin
Ganesan Senthilvel7-Jul-12 3:27
MemberGanesan Senthilvel7-Jul-12 3:27 
GeneralMy vote of 5 Pin
Farhan Ghumra27-Jun-12 3:24
professionalFarhan Ghumra27-Jun-12 3:24 
GeneralRe: My vote of 5 Pin
Technoses27-Jun-12 7:43
MemberTechnoses27-Jun-12 7:43 
GeneralMy vote of 4 Pin
zhxhdean26-Jun-12 21:22
Memberzhxhdean26-Jun-12 21:22 
GeneralRe: My vote of 4 Pin
Technoses27-Jun-12 7:44
MemberTechnoses27-Jun-12 7:44 
GeneralMy vote of 5 Pin
Carsten V2.026-Jun-12 10:32
MemberCarsten V2.026-Jun-12 10:32 
GeneralMy vote of 5 Pin
Prosan22-Jun-12 22:40
MemberProsan22-Jun-12 22:40 
SuggestionA suggestion Pin
stingrayweb9-May-12 9:31
Memberstingrayweb9-May-12 9:31 
GeneralRe: A suggestion Pin
Technoses9-Jul-12 19:02
MemberTechnoses9-Jul-12 19:02 
GeneralReason for my vote of 5 excellent. short, concise, gets the ... Pin
davecal3-Jan-12 10:00
Memberdavecal3-Jan-12 10:00 
GeneralRe: Thanks Pin
Technoses5-Jan-12 19:32
MemberTechnoses5-Jan-12 19:32 
GeneralReason for my vote of 5 Very Good ... & thanks Pin
Member 43208442-Jan-12 8:15
MemberMember 43208442-Jan-12 8:15 
GeneralRe: Thanks for your appreciation Pin
Technoses5-Jan-12 19:32
MemberTechnoses5-Jan-12 19:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.