Click here to Skip to main content
15,884,836 members
Articles / Web Development / ASP.NET

Resume Support for File Downloads

Rate me:
Please Sign up or sign in to vote.
4.81/5 (35 votes)
9 Jul 2012CPOL1 min read 98.4K   3.5K   58  
Partial downloading of files.
using System;
using System.Collections.Generic;
using System.Text;

namespace DownloaderBase
{
    class Program
    {
        static System.Collections.Specialized.NameValueCollection q;
        static void Main(string[] args)
        {
            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]);                
            }
        }
        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);

        }
        
        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, System.IO.FileShare.ReadWrite);
            else
                saveFileStream = new System.IO.FileStream(sDestinationPath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);



            System.Net.HttpWebRequest hwRq;
            System.Net.HttpWebResponse hwRes;
            hwRq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(sSourceURL);
            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];

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



        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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#
- ASP.NET
- ASP.NET AJAX
- SQL Server 2005
- ADO .NET
- 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