Click here to Skip to main content

Resume Support Downloader

Introduction

As we know about downloading a file from website is just by click on a link.but if we want to develop an application which download a list of file for us and which is usefull for our programme then we use web request to download files. But by network interruption, downloading a file is a problem for us when the size of file is large. Here we know how to download a file partially so that the we can download a big file easily.

Background

This process uses HttpWebRequest and HttpWebResponse classes of .net. which one who know that how the above classes and its methods works can do it easily.

Using the code

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

//
namespace DownloaderBase
{
    class Program
    {
        static System.Collections.Specialized.NameValueCollection q;
        static void Main(string[] args)
        {

This Console Application takes arguments for Download Source Path and Destination Path

    if (args != null)
    {
        if (args.Length > 0)
        {
            q = new System.Collections.Specialized.NameValueCollection();
            try
            {
                //Console.WriteLine(args[0]);
                //Console.ReadKey();
                //return;

                SetArguments(args[0]);
                DownloadFile();
                Console.ReadKey();
                return;
            }
            catch (Exception eXc)
            {
                Console.WriteLine("Error :" + eXc.Message);
                Console.WriteLine("Stack :" + eXc.StackTrace);
            }

        }
    }
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("Call with Arguments");
    sb.AppendLine("");
    sb.AppendLine("");
    sb.AppendLine("srcUrl: Source URL Path");
    sb.AppendLine("");
    sb.AppendLine("");
    sb.AppendLine("DestPath: Destination File Path Where it to Save");
    Console.WriteLine(sb.ToString());
    Console.ReadKey();
}
static void SetArguments(string sArguments)
{
    string[] sArgs = sArguments.Split(new string[] { "/:" }, StringSplitOptions.RemoveEmptyEntries);
    int iDx = 0;
    foreach (string ss in sArgs)
    {
        iDx++;
        string[] sVl = ss.Split('=');
        q.Add(sVl[0], sVl[1]);
    }
}

This Function Used to Download File From Web

  static void DownloadFile()
{

    string sDest= q["DestPath"];
    string sUrl = q["srcUrl"];
    Console.WriteLine("Destination Path :" + sDest);
    Console.WriteLine("Source File Path :" + sUrl);

    DownloadFile(sUrl, sDest);

}

We Take two arguments One is Source URL and Second is Absolute Destination Path

 static void DownloadFile(string sSourceURL, string sDestinationPath)
{
    long iFileSize = 0;
    int iBufferSize = 1024;
    iBufferSize *= 1000;
    long iExistLen = 0;
    System.IO.FileStream saveFileStream;

Here, if any file is in mid stage of process in download and not completely downloaded then we store the length of the existing file and store in iExistLen..

if (System.IO.File.Exists(sDestinationPath))
{
    System.IO.FileInfo fINfo = new System.IO.FileInfo(sDestinationPath);
    iExistLen = fINfo.Length;
}

If file exist then we create stream write file with append mode so that next downloaded data will append with existing file. Other wise we create it with create mode that will create new file object to download.

if (iExistLen > 0)
    saveFileStream = new System.IO.FileStream(sDestinationPath, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
else
    saveFileStream = new System.IO.FileStream(sDestinationPath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);

We create web request to download that file..

System.Net.HttpWebRequest hwRq;
System.Net.HttpWebResponse hwRes;
hwRq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(sSourceURL);

the main process to download that file from mis is that we create request for file and use AddRange method which takes an integer argument that how much file has been downloaded...

///...
hwRq.AddRange((int)iExistLen);
System.IO.Stream smRespStream;
hwRes = (System.Net.HttpWebResponse)hwRq.GetResponse();
smRespStream = hwRes.GetResponseStream();

iFileSize = hwRes.ContentLength;

int iByteSize;
byte[] downBuffer = new byte[iBufferSize];

And after that we download this file from the last point. we can us iBufferSize to control Downloading speed.

            while ((iByteSize = smRespStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
            {
                saveFileStream.Write(downBuffer, 0, iByteSize);
            }



        }

    }
}


//

Web02 | 2.8.160208.1 | Advertise | Privacy
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service