Condividi tramite


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

Comments

  • Anonymous
    December 01, 2004
    The comment has been removed

  • Anonymous
    December 03, 2004
    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.

  • Anonymous
    December 05, 2004
    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.

  • Anonymous
    January 04, 2006
    h,
    can we use th e ftpwebrequest to resume file upload.if yes please tell me how.
    sushma

  • Anonymous
    January 04, 2006
    h,
    can we use th e ftpwebrequest to resume file upload.if yes please tell me how.
    sushma

  • Anonymous
    September 14, 2006
    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?

  • Anonymous
    May 10, 2007
    when run this code i got this errorThe remote server returned an error: (407) Proxy Authentication Required.help if u can

  • Anonymous
    July 25, 2008
    This is a current compile of the team's existing blogs on FtpWebRequest. I am going to update it periodically

  • Anonymous
    July 29, 2008
    Great solution man, Its really solve my problem

  • Anonymous
    September 11, 2008
    Have something for file upload on ftp and resume task if connection got broken

  • Anonymous
    June 05, 2009
    Hi Sushma/Ravi,Have you find any solution to continue the broken upload ?  If so, please share your solution.thanks,gurunath

  • Anonymous
    June 21, 2009
    For everyone asking for upload resume:Get the current filesize from the directory listing Use this as position for the filestream Use WebRequestMethods.Ftp.AppendFile

  • Anonymous
    September 17, 2009
    the article is very useful thank you very much

  • Anonymous
    September 22, 2009
    can any find the solution for a broken upload

  • Anonymous
    October 05, 2009
    can anyone please tell me how to resume a file upload using http protocol?

  • Anonymous
    October 20, 2009
    Hi, excuse me... can you post an example for upload ? what's mean "2. Use this as position for the filestream" ??Tnx for answers.

  • Anonymous
    November 23, 2009
    When I try something similar for uploadI always get 505 File unavailable after setting the ContentOffset and making the request, any ideas?

  • Anonymous
    October 14, 2010
    Does this solution work when the request.EnableSsl = true to use the ftps?

  • Anonymous
    June 22, 2011
    But i also want to do same with Uploading, do you have any idea?

  • Anonymous
    December 26, 2012
    Wonderful post, thank you. This was helpful to me when I was writing a class to download large files over FTP.