Solved

Auto-resuming HTTP download C#

Posted on 2007-04-09
1
3,023 Views
Last Modified: 2013-12-17
In C#, is there a simple way to resume an download over http, transparantly?

For example, i call a function to download a file x to drive c:. In the middle of the operation, I close the downloading program. The program would abort the transfer, but not delete the partially downladed file x.

I start the program, and once again call the function to download the same file x to driver c:. The function would realize that there is already a part of file x present on drive c: and then resume the download.

Anything simple in .NET 2.0 library that would allow that to happen?
0
Comment
Question by:krupini
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 11

Accepted Solution

by:
dready earned 500 total points
ID: 18878963
The way to go, is first to check if the file allready exists on you computer, and if so, determine the length. What you want to do, is something like


if (File.Exists(txtPath.Text))
{
    startPoint = new FileInfo(txtPath.Text).Length;
}
else
{
    startPoint = 0;
}

Then you download using the startPoint value. Your download function could look like the following.
I copied the relevant code from the following URL: http://www.geekpedia.com/tutorial196_Creating-an-advanced-download-manager-in-Csharp.html



private void Download(object startPoint)

{

    try

    {
        // Put the object argument into an int variable

        int startPointInt = Convert.ToInt32(startPoint);

        // Create a request to the file we are downloading

        webRequest = (HttpWebRequest)WebRequest.Create(txtUrl.Text);

        // Set the starting point of the request

        webRequest.AddRange(startPointInt);

 

        // Set default authentication for retrieving the file

        webRequest.Credentials = CredentialCache.DefaultCredentials;

        // Retrieve the response from the server

        webResponse = (HttpWebResponse)webRequest.GetResponse();

        // Ask the server for the file size and store it

        Int64 fileSize = webResponse.ContentLength;

 

        // Open the URL for download

        strResponse = webResponse.GetResponseStream();

 

        // Create a new file stream where we will be saving the data (local drive)

        if (startPointInt == 0)

        {

            strLocal = new FileStream(txtPath.Text, FileMode.Create, FileAccess.Write, FileShare.None);

        }

        else

        {

            strLocal = new FileStream(txtPath.Text, FileMode.Append, FileAccess.Write, FileShare.None);

        }

 

        // It will store the current number of bytes we retrieved from the server

        int bytesSize = 0;

        // A buffer for storing and writing the data retrieved from the server

        byte[] downBuffer = new byte[2048];

 

        // Loop through the buffer until the buffer is empty

        while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)

        {

            // Write the data from the buffer to the local hard drive

            strLocal.Write(downBuffer, 0, bytesSize);

            // Invoke the method that updates the form's label and progress bar

            this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, fileSize + startPointInt });

 

            if (goPause == true)

            {

                break;

            }

        }

    }

    finally

    {

        // When the above code has ended, close the streams

        strResponse.Close();

        strLocal.Close();

    }

}


http://www.geekpedia.com/tutorial196_Creating-an-advanced-download-manager-in-Csharp.html
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Open a link in vb.net 2 26
Tracing a live website down to the files which support it 2 30
C# XML Get Values 4 34
Header Font Size in Grid View 6 24
Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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