Click here to Skip to main content
13,350,861 members (53,790 online)
Click here to Skip to main content
Add your own
alternative version


56 bookmarked
Posted 29 Dec 2011

Resume Support for File Downloads

, 9 Jul 2012
Rate this:
Please Sign up or sign in to vote.
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)


About the Author

Software Developer (Senior) Super Eminent Softwares
India India
This member doesn't quite have enough reputation to be able to display their biography and homepage.
Group type: Organisation

42 members

You may also be interested in...


Comments and Discussions

GeneralMy vote of 4 Pin
zhxhdean26-Jun-12 22:22
memberzhxhdean26-Jun-12 22:22 
GeneralRe: My vote of 4 Pin
Technoses27-Jun-12 8:44
groupTechnoses27-Jun-12 8:44 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 9 Jul 2012
Article Copyright 2011 by Technoses
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid