Welcome to MSDN Blogs Sign in | Join | Help

adarsh's blog

Adarsh Khare works at Microsoft. Everything here, though, is his personal opinion and is not read or approved by Microsoft before it is posted. No warranties or other guarantees will be offered as to the quality of the opinions or anything else offered here.
Resuming broken file download with FtpWebRequest class

This post is valid for .Netframeworks 2.0

When we are downloading a large file from ftp site and connection got broken in between, on next attempt you would be interested in downloading the rest of the file content instead of full file. FtpWebRequest class have a nice way to meet this requirement. You could use the FtpWebRequest.ContentOffset property to specify the starting position for file to download. See code example below

Following is the code for downloading the file

public static void ResumeFtpFileDownload(Uri sourceUri, string destinationFile)
{
        FileInfo file = new FileInfo(destinationFile);
        FileStream localfileStream ;
        FtpWebRequest request = WebRequest.Create(sourceUri) as FtpWebRequest;
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        if (file.Exists)
        {
            request.ContentOffset = file.Length;
            localfileStream = new FileStream(destinationFile, FileMode.Append, FileAccess.Write);
        }
        else
        {
               localfileStream = new FileStream(destinationFile, FileMode.Create, FileAccess.Write);
        }
        WebResponse response = request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        byte[] buffer = new byte[1024];
        int bytesRead = responseStream.Read(buffer, 0, 1024);
        while (bytesRead != 0)
        {
               localfileStream.Write(buffer, 0, bytesRead);
               bytesRead = responseStream.Read(buffer, 0, 1024);
        }
        localfileStream.Close();
        responseStream .Close();
 }

This posting is provided "AS IS" with no warranties, and confers no rights

Posted: Wednesday, December 01, 2004 3:04 PM by adarshk

Comments

Luc Cluitmans said:

Is there also a similar functionality for HTTP requests?

Or can you give a sample of how to continue an aborted HTTP request (assuming it is a plain file download, and the server supports byte ranges)?
# December 2, 2004 3:34 AM

adarshk said:

Yes you can, HttpWebRequest class have AddRange() method for it, in above sample you need to do request.AddRange(file.Length); instead of setting ContentOffset, rest of the code would be similar.
# December 3, 2004 7:44 PM

Luc Cluitmans said:

Thanks for your reply!

While playing around with this method (in VS2003, using the documentation from MSDN library CDs from April 2004), I noticed that the documentation for "HttpWebRequest.AddRange(int)" is either simply wrong, or stated in such an obscure way that I couldn't get the truth out of it. The part of the remarks, where it describes the effect of positive versus negative arguments seems to be wrong for both cases.

The correct description is best given by an example:
In a request for a 1000 byte document:

AddRange(300) will return all bytes from byte 300 (bytes 300-999), that is: 700 bytes in total.
[this btw matches the given description for the *negative* range!]

AddRange(-300) will return the last 300 bytes (bytes 700-999), that is: 300 bytes in total.

It may be useful to refer to the correct section of the RFC in the docs. Also, the docs should mention that, if the Range request is successful, the return status will be 206 rather than 200, and the response will have a 'Content-Range' header that tells you what range exactly is returned. If a 200 response is returned, the Range request has been ignored and you got the full document instead.
# December 6, 2004 3:55 AM

sushma said:

h,
can we use th e ftpwebrequest to resume file upload.if yes please tell me how.
sushma
# January 5, 2006 6:27 AM

sushma said:

h,
can we use th e ftpwebrequest to resume file upload.if yes please tell me how.
sushma
# January 5, 2006 6:28 AM

Michael said:

I tested the code above, as to what I discover:

FtpWebRequest request = WebRequest.Create(sourceUri) as FtpWebRequest;

is not a valid cast, when you set the breakpoint there, the returned request is null if you're using http protocal

HttpWebRequest.AddRange is not  working as well, whether it is AddRange(300) or AddRange(-300), check the returned buffer, you'll see the result is the same

and the  code at Microsoft site:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemNetHttpWebRequestClassTopic.asp

is not working at all, any ideas?
# September 14, 2006 9:56 AM

ROHIT said:

when run this code i got this error

The remote server returned an error: (407) Proxy Authentication Required.

help if u can

# May 11, 2007 5:37 AM

Network Class Library Team (System.Net) said:

This is a current compile of the team's existing blogs on FtpWebRequest. I am going to update it periodically

# July 25, 2008 9:23 PM

Ravi said:

Great solution man, Its really solve my problem

# July 30, 2008 2:01 AM

Ravi said:

Have something for file upload on ftp and resume task if connection got broken

# September 12, 2008 2:38 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker