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);
               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);
        responseStream .Close();

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

Comments (20)
  1. 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)?

  2. adarshk says:

    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.

  3. 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.

  4. sushma says:


    can we use th e ftpwebrequest to resume file upload.if yes please tell me how.


  5. sushma says:


    can we use th e ftpwebrequest to resume file upload.if yes please tell me how.


  6. Michael says:

    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:

    is not working at all, any ideas?

  7. ROHIT says:

    when run this code i got this error

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

    help if u can

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

  9. Ravi says:

    Great solution man, Its really solve my problem

  10. Ravi says:

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

  11. gurunath says:

    Hi Sushma/Ravi,

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



  12. 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

  13. karthik says:

    the article is very useful thank you very much

  14. karthik says:

    can any find the solution for a broken upload

  15. Ratna says:

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

  16. Franz says:

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

    Tnx for answers.

  17. TimothyP says:

    When I try something similar for upload

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

  18. Roger says:

    Does this solution work when the request.EnableSsl = true to use the ftps?

  19. Hammad says:

    But i also want to do same with Uploading, do you have any idea?

  20. TGP1994 says:

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

Comments are closed.

Skip to main content