Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading files Using FtpWebRequest

I'm trying to download a file using FtpWebRequest.

private void DownloadFile(string userName, string password, string ftpSourceFilePath, string localDestinationFilePath)
{
    int bytesRead = 0;
    byte[] buffer = new byte[1024];

    FtpWebRequest request = CreateFtpWebRequest(ftpSourceFilePath, userName, password, true);
    request.Method = WebRequestMethods.Ftp.DownloadFile;

    Stream reader = request.GetResponse().GetResponseStream();
    BinaryWriter writer = new BinaryWriter(File.Open(localDestinationFilePath, FileMode.CreateNew));

    while (true)
    {
        bytesRead = reader.Read(buffer, 0, buffer.Length);

        if (bytesRead == 0)
            break;

        writer.Write(buffer, 0, bytesRead);
    }        
}

It uses this CreateFtpWebRequest method I created:

private FtpWebRequest CreateFtpWebRequest(string ftpDirectoryPath, string userName, string password, bool keepAlive = false)
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ftpDirectoryPath));

    //Set proxy to null. Under current configuration if this option is not set then the proxy that is used will get an html response from the web content gateway (firewall monitoring system)
    request.Proxy = null;

    request.UsePassive = true;
    request.UseBinary = true;
    request.KeepAlive = keepAlive;

    request.Credentials = new NetworkCredential(userName, password);

    return request;
}

It downloads it. But the information is always corrupted. Anyone know what's going on?

like image 319
Rick Eyre Avatar asked Sep 20 '12 19:09

Rick Eyre


People also ask

How do I automatically download files from an FTP server?

Go to File >> New Connection Profile. On the create connection profile dialog, select the Automated Profile option. On the next page, provide the details required to connect to the FTP server. On the action rules page, select the option to download files from your FTP server.

How do I download using Filezilla?

Download files using Filezilla Downloading files from your remote server to your local computer works in a similar way. Navigate to the file or folder you wish to download in the right pane. Highlight the file or folder, right-click, and select Download.


1 Answers

Just figured it out:

    private void DownloadFile(string userName, string password, string ftpSourceFilePath, string localDestinationFilePath)
    {
        int bytesRead = 0;
        byte[] buffer = new byte[2048];

        FtpWebRequest request = CreateFtpWebRequest(ftpSourceFilePath, userName, password, true);
        request.Method = WebRequestMethods.Ftp.DownloadFile;

        Stream reader = request.GetResponse().GetResponseStream();
        FileStream fileStream = new FileStream(localDestinationFilePath, FileMode.Create);

        while (true)
        {
            bytesRead = reader.Read(buffer, 0, buffer.Length);

            if (bytesRead == 0)
                break;

            fileStream.Write(buffer, 0, bytesRead);
        }
        fileStream.Close();       
    }

Had to use a FileStream instead.

like image 66
Rick Eyre Avatar answered Oct 20 '22 11:10

Rick Eyre