How to download FTP files with automatic resume in case of disconnect
Asked Answered
C

2

4

I can download FTP files, but the download code does not have a resume facility and multi part files download. Because there are file larger than 500 MB, I can't download them continuously, because the connection gets closed and it starts download from beginning. I wanted my code a resume facility if it gets disconnected.

The code I am using is:

public string[] GetFileList()
{
    string[] downloadFiles;
    StringBuilder result = new StringBuilder();
    FtpWebRequest reqFTP;
    try
    {
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(
            new Uri("ftp://" + ftpServerIP + "/"));
        reqFTP.UseBinary = true;
        reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
        reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
        WebResponse response = reqFTP.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        //MessageBox.Show(reader.ReadToEnd());
        string line = reader.ReadLine();
        while (line != null)
        {
            result.Append(line);
            result.Append("\n");
            line = reader.ReadLine();
        }
        result.Remove(result.ToString().LastIndexOf('\n'), 1);
        reader.Close();
        response.Close();
        //MessageBox.Show(response.StatusDescription);
        return result.ToString().Split('\n');
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
        downloadFiles = null;
        return downloadFiles;
    }
}

private void Download(string filePath, string fileName)
{
    FtpWebRequest reqFTP;
    try
    {
        //filePath = <<The full path where the file is to be created.>>, 
        //fileName = <<Name of the file to be created
        //             (Need not be the name of the file on FTP server).>>
        FileStream outputStream =
            new FileStream(filePath + "\\" + fileName, FileMode.Create);

        reqFTP = (FtpWebRequest)FtpWebRequest.Create(
            new Uri("ftp://" + ftpServerIP + "/" + fileName));
        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
        reqFTP.UseBinary = true;
        reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
        FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
        Stream ftpStream = response.GetResponseStream();
        long cl = response.ContentLength;
        int bufferSize = 2048;
        int readCount;
        byte[] buffer = new byte[bufferSize];

        readCount = ftpStream.Read(buffer, 0, bufferSize);
        while (readCount > 0)
        {
            outputStream.Write(buffer, 0, readCount);
            readCount = ftpStream.Read(buffer, 0, bufferSize);
        }

        ftpStream.Close();
        outputStream.Close();
        response.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Clementeclementi answered 13/6, 2011 at 13:33 Comment(0)
R
8

Before you start downloading check for the existence of the file on the local filesystem. If it exists, then get the size and use that for the ContentOffset member of the FtpWebRequest object. This functionality may not be supported by the FTP server, though.

Reform answered 13/6, 2011 at 13:41 Comment(0)
H
0

A native implementation of FTP download resumption using the FtpWebRequest:

bool resume = false;
do
{
    try
    {
        FileMode mode = resume ? FileMode.Append : FileMode.Create;
        resume = false;
        using (Stream fileStream = File.Open(@"C:\local\path\file.dat", mode))
        {
            var url = "ftp://ftp.example.com/remote/path/file.dat";
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
            request.Credentials = new NetworkCredential("username", "password");
            request.Method = WebRequestMethods.Ftp.DownloadFile;
            request.ContentOffset = fileStream.Position;
            using (Stream ftpStream = request.GetResponse().GetResponseStream())
            {
                ftpStream.CopyTo(fileStream);
            }
        }
    }
    catch (WebException)
    {
        resume = true;
    }
}
while (resume);

Or use an FTP library that can resume the transfer automatically.

For example WinSCP .NET assembly does. With it, a resumable download is as trivial as:

// Setup session options
var sessionOptions = new WinSCP.SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "ftp.example.com",
    UserName = "user",
    Password = "mypassword"
};

using (var session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // Resumable download
    session.GetFileToDirectory("/home/user/file.zip", @"C:\path");
}

(I'm the author of WinSCP)

Hudnut answered 11/2, 2021 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.