-2

I have an issue sending big file to an FTP site and I would like to check the file size after the transfer (sometimes it works sometimes it fails). The transfer is within an SSIS and I'm using Dts.Connections in C#.

My code:

public long TransferFile(string file)
{
    long filesize = 0L;

    try
    {
        string[] newfile = new[] { file };

        ConnectionManager ftpCM = Dts.Connections["ftp_server"];
        string remoteDir = Dts.Variables["FtpWorkingDirectory"].Value.ToString();

        FtpClientConnection ftpClient = new FtpClientConnection(ftpCM.AcquireConnection(null));
        ftpClient.UsePassiveMode = true;
        ftpClient.Connect();
        ftpClient.Retries = 10;
        ftpClient.SetWorkingDirectory(remoteDir);

        ftpClient.SendFiles(newfile, remoteDir, true, false);

        ftpClient.Close();
    }
    catch (Exception ex)
    {
        throw ex;
    }

    return filesize;
}

I found examples using FtpWebRequest but I don't have the ftp uri available so I don't see how to use this method. How can I get this file size?

UPDATE: Adding this:

        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpCM.ConnectionString + remoteDir + "/" + file));
        request.Proxy = null;

        DtsProperty ServerUsername = ftpCM.Properties["ServerUserName"];
        DtsProperty ServerPassword = ftpCM.Properties["ServerPassword"];
        request.Credentials = new NetworkCredential(ServerUsername.GetValue(ftpCM).ToString(), ServerPassword.GetValue(ftpCM).ToString());
        request.Method = WebRequestMethods.Ftp.GetFileSize;

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        filesize = response.ContentLength;
        response.Close();

I get error: The property ServerPassword is write-only.

pdube
  • 593
  • 1
  • 11
  • 26
  • The ftpCM should be the uri. – Kevin Raffay May 05 '17 at 18:42
  • *"I don't have the ftp uri available"* - What do you mean by that? The URL is just another format to provide the connection information that you need for FTP connection anyway! - like `ftp://username:password@hostname` or even `ftp://hostname`, if your provide username/password separately via `request.Credentials = new NetworkCredential(...)`. – Martin Prikryl May 05 '17 at 18:48
  • Where do you get the "The property ServerPassword is write-only."? - exactly! – Martin Prikryl May 05 '17 at 19:13

1 Answers1

0

The final solution was to store the username and password in Variables instead of connection parameters.

And with the following code:

public long TransferFile(string file)
{
    long filesize = 0L;

    try
    {
        string[] newfile = new[] { file };

        ConnectionManager ftpCM = Dts.Connections["ftp_server"];
        string remoteDir = Dts.Variables["FtpWorkingDirectory"].Value.ToString();
        string ServerUsername = Dts.Variables["ServerUsername"].Value.ToString();
        string ServerPassword = Dts.Variables["ServerPassword"].Value.ToString();

        FtpClientConnection ftpClient = new FtpClientConnection(ftpCM.AcquireConnection(null));
        ftpClient.UsePassiveMode = true;
        ftpClient.ServerUserName = ServerUsername;
        ftpClient.ServerPassword = ServerPassword;

        ftpClient.Connect();
        ftpClient.Retries = 10;
        ftpClient.SetWorkingDirectory(remoteDir);

        ftpClient.SendFiles(newfile, remoteDir, true, false);

        ftpClient.Close();

        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpCM.ConnectionString + remoteDir + "/" + Path.GetFileName(file)));
        request.Proxy = null;

        request.Credentials = new NetworkCredential(ServerUsername, ServerPassword);
        request.Method = WebRequestMethods.Ftp.GetFileSize;

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        filesize = response.ContentLength;
        response.Close();
    }
    catch (Exception ex)
    {
        throw ex;
    }

    return filesize;
}
pdube
  • 593
  • 1
  • 11
  • 26