Using FTP File as Input Provider

The sample C# code below demonstrates how to fetch a CSV file from a remote FTP server (using third-party library) and loading its content as input provider for the Data List command. Sample agent is attached to this article.

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using Sequentum.ContentGrabber.Api;
public class Script
{
//See help for a definition of DataProviderArguments.
  public static CustomDataStorage ProvideData(DataProviderArguments args)
  {
    var server = "ftp://ftp.dlptest.com/";
    var port = 21;
    var username = "dlpuser@dlptest.com";
    var password = "fLDScD4Ynth0p4OJ6bW6qCxjh";
    var remoteFilePath = "testfolder/testInput.csv";

    var cds = new CustomDataStorage();
    var tempFile = Path.GetTempFileName();
    FluentFTP.FtpClient client = null;
    try
    {
      using (client = new FluentFTP.FtpClient(server, port, username, password))
      {
// Connect to the server
        client.Connect();
// Download file
        if (client.DownloadFile(tempFile, remoteFilePath, true, FluentFTP.FtpVerify.None, null))
        {
// Load downloaded csv file.
          cds = args.ScriptUtilities.LoadCsvFile(tempFile, true);
        }
        else
        {
          throw new Exception("Failed to download file: " + remoteFilePath);
        }
      }
    }
    catch (Exception ex)
    {
      throw ex;
    }
    finally
    {
// Delete temporary file
      if (File.Exists(tempFile)) File.Delete(tempFile);
      if (client != null) client.Disconnect();
    }

    return cds;
  }
}

Required third-party library: FluentFTP. Download the DLL into your Agent's "Assembly" folder, and add "FluentFTP.dll" in your Agent's Assembly References. The DLL is already added and set up in the attached sample agent.

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.