logo

Loading file from context to ftp-server

In the article, you can find an example of how to:

  • load a file to a specific folder on the FTP server;
  • check folders on the FTP server;
  • create a folder if it is missing.

To work correctly you need to use the namespace:

using System.Net;

The text of the script is shown below:

#region FTP
  // Username to connect to the FTP server
  string userName = "username";
  // Password to connect to the FTP server
  string password = "password";
   
  public void FTPMain (Context context)
  {
   // Link to FTP
   string uri = "ftp: //" + "ftp.media.moyareklama.ru" + "/";
   // Name of the folder in which you want to download the file
   string nameFolder = "NameFolder";
   string answer = "";
   // Check whether there is a folder on the FTP server
   if (FTPCheckFolder (uri, nameFolder))
   {
    uri + = "NameFolder" + "/";
    // Load the file and obtain the path to the file on the FTP server
    answer = FTPUploadFile (uri, context.File.ContentFilePath, context.File.Name);
   }
   else
   {
    uri + = "NameFolder";
    // Create a folder on the FTP server
    FTPCreateFolder (uri);
    uri + = "/";
    // Load the file and obtain the path to the file on the FTP server
    answer = FTPUploadFile (uri, context.File.ContentFilePath, context.File.Name);
   }
   // Display the path to the file in the console 
   Console.WriteLine (answer);
  }
   
  protected bool FTPCheckFolder (string uri, string nameFolder)
  {
   FtpWebRequest reqFTP;
   // Create an object FtpWebRequest
   reqFTP = (FtpWebRequest) FtpWebRequest.Create (new Uri (uri));
   // Specify the account
   reqFTP.Credentials = new NetworkCredential (userName, password);
   reqFTP.KeepAlive = false;
   // Select the method that returns a detailed list of files on the ftp server
   reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
   reqFTP.UseBinary = true;
   // Get the response from the FTP server
   FtpWebResponse resp = (FtpWebResponse) reqFTP.GetResponse ();
   // Get the data stream
   Stream responseStream = resp.GetResponseStream ();
   StreamReader reader = new StreamReader (responseStream);
   // Read data from stream
   var contents = reader.ReadToEnd ();
   // Close streams
   reader.Close ();
   resp.Close ();
   // Divide the resulting string into an array of rows, check whether there is a folder named nameFolder
   if (contents.Replace ( "\ r \ n", "") .Split ( '') .Any (c => c == nameFolder))
    return true;
   return false;
  }
   
  protected string FTPUploadFile (string uri, string filePath, string fileName)
  {
   FileInfo fileInf = new FileInfo (filePath);
   FtpWebRequest reqFTP;
   // Create an object FtpWebRequest
   reqFTP = (FtpWebRequest) FtpWebRequest.Create (new Uri (uri + fileName));
   // Specify the account
   reqFTP.Credentials = new NetworkCredential (userName, password);
   reqFTP.KeepAlive = false;
   // Select the file boot method
   reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
   // File Transfer Type
   reqFTP.UseBinary = true;
   // Specify file size
   reqFTP.ContentLength = fileInf.Length;
   // 2 Kbytes Buffer 
   int buffLength = 2048;
   byte [] buff = new byte [buffLength];
   int contentLen;
   // Open the file stream
   FileStream fs = fileInf.OpenRead ();
   try
   {
    Stream strm = reqFTP.GetRequestStream ();
    // Read from the stream by 2 Kbytes
    contentLen = fs.Read (buff, 0, buffLength);
    // Until the file in not read completely  
    while (contentLen! = 0)
    {
     strm.Write (buff, 0, contentLen);
     contentLen = fs.Read (buff, 0, buffLength);
    }
    // Close the streams
    strm.Close ();
    fs.Close ();
   }
   catch (Exception ex)
   {
    Console.WriteLine (ex.Message);
   }
   return uri + fileName;
  }
   
  protected void FTPCreateFolder (string uri)
  {
   FtpWebRequest reqFTP;
   // Create an object FtpWebRequest
   reqFTP = (FtpWebRequest) FtpWebRequest.Create (new Uri (uri));
   // Specify the account
   reqFTP.Credentials = new NetworkCredential (userName, password);
   reqFTP.KeepAlive = false;
   // Select the method for creating a folder
   reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
   reqFTP.UseBinary = true;
   // Get the response from the FTP server
   FtpWebResponse resp = (FtpWebResponse) reqFTP.GetResponse ();
   // Close the stream
   resp.Close ();
  }
  #endregion