Resuming broken file download with FtpWebRequest class

Published 01 December 04 03:04 PM | adarshk 

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

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

Comments

# Luc Cluitmans said on December 2, 2004 3:34 AM:
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)?
# adarshk said on December 3, 2004 7:44 PM:
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.
# Luc Cluitmans said on December 6, 2004 3:55 AM:
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.
# sushma said on January 5, 2006 6:27 AM:
h,
can we use th e ftpwebrequest to resume file upload.if yes please tell me how.
sushma
# sushma said on January 5, 2006 6:28 AM:
h,
can we use th e ftpwebrequest to resume file upload.if yes please tell me how.
sushma
# Michael said on September 14, 2006 9:56 AM:
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?
# ROHIT said on May 11, 2007 5:37 AM:

when run this code i got this error

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

help if u can

# Network Class Library Team (System.Net) said on July 25, 2008 9:23 PM:

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

# Ravi said on July 30, 2008 2:01 AM:

Great solution man, Its really solve my problem

# Ravi said on September 12, 2008 2:38 AM:

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

# gurunath said on June 5, 2009 6:33 PM:

Hi Sushma/Ravi,

Have you find any solution to continue the broken upload ?  If so, please share your solution.

thanks,

gurunath

# Jasper Seidel said on June 22, 2009 3:47 AM:

For everyone asking for upload resume:

1. Get the current filesize from the directory listing

2. Use this as position for the filestream

3. Use WebRequestMethods.Ftp.AppendFile

# karthik said on September 18, 2009 3:04 AM:

the article is very useful thank you very much

# karthik said on September 22, 2009 9:22 AM:

can any find the solution for a broken upload

# Ratna said on October 5, 2009 10:02 AM:

can anyone please tell me how to resume a file upload using http protocol?

# Franz said on October 20, 2009 8:05 AM:

Hi, excuse me... can you post an example for upload ? what's mean "2. Use this as position for the filestream" ??

Tnx for answers.

# TimothyP said on November 23, 2009 8:03 PM:

When I try something similar for upload

I always get 505 File unavailable after setting the ContentOffset and making the request, any ideas?

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

Search

This Blog

Syndication

Page view tracker