SoFunction
Updated on 2025-03-07

C# Example of implementing FTP upload data

1. By uploading files with FTP, the first thing to do is to establish an FTP connection. Generally, establish an FTP connection and you need to know the information about FTP configuration. Generally, a file is created in the bean for recording, which generally requires an FTP address, login username and login password. Then access and read through other pages. The code style is as follows:

class ServiceFileInfo
  {
    // service1
    public static string txtFilePath = @"ftp://12.128.128.01/FileName/";
    //userid & password
    public static string txtUID = "username";
    public static string txtPWD = "password";
  }

2. Read the information of the file below the bean file through the main method to establish an FTP connection. Here you also need to know clearly the path (Path) and file name (FileName) of the file you upload. Based on this information, the main method calls another file written in the Bean (this .cs file mainly writes some operation methods about FTP) to perform FTP access operations.

  • The main method calls FTP operation code
ExecutionResult exeRes = (textFilePath, txtUID, txtPWD, Path + "/" + FileName + ".txt");//.txtThe suffix name of the file
  • Methods about FTP operations in bean files
public ExecutionResult UploadFile(string vIMSPath, string vUID, string vPassword, string vLocalPath)
    {
      ExecutionResult result = new ExecutionResult();
      result = connectState(vIMSPath, vUID, vPassword, vLocalPath);//Calling the following code method      if ()
      {
        (vLocalPath);
      }
      return result;
    }
  • connectState() method
public static ExecutionResult connectState(string vIMSPath, string vUID, string vPassword, string fileName)
    {
      string operater = "";
      bool Flag = false;
      ExecutionResult result;
      result = new ExecutionResult();
      lock (lockObj)
      {
        try
        {
          operater = "Connet to FTP";
          FTPOperation ftp = new FTPOperation(new Uri(vIMSPath), vUID, vPassword);
          operater = "Upload file";
          Flag = (fileName, (fileName), true);
          if (Flag)
          {
             = true;
             = "Send to server OK";
          }
        }
        catch (Exception ex)
        {
           = false;
           = "Mail";
           = operater + ":" + ;
        }
      }
      return result;
    }
  • UploadFile() method
public bool UploadFile(string LocalFullPath, string RemoteFileName, bool OverWriteRemoteFile)
    {
      bool result;
      try
      {
        bool flag = !(RemoteFileName) || !((LocalFullPath)) || !((LocalFullPath));
        if (flag)
        {
          throw new Exception("Illegal file name or directory name!");
        }
        bool flag2 = (LocalFullPath);
        if (!flag2)
        {
          throw new Exception("The local file does not exist!");
        }
        FileStream fileStream = new FileStream(LocalFullPath, , );
        byte[] array = new byte[];
        (array, 0, (int));
        ();
        result = (array, RemoteFileName, OverWriteRemoteFile);
      }
      catch (Exception ex)
      {
         = ();
        throw ex;
      }
      return result;
    }



public bool UploadFile(byte[] FileBytes, string RemoteFileName)
    {
      bool flag = !(RemoteFileName);
      if (flag)
      {
        throw new Exception("Illegal file name or directory name!");
      }
      return (FileBytes, RemoteFileName, false);
    }


public bool UploadFile(byte[] FileBytes, string RemoteFileName, bool OverWriteRemoteFile)
    {
      bool result;
      try
      {
        bool flag = !(RemoteFileName);
        if (flag)
        {
          throw new Exception("Illegal file name!");
        }
        bool flag2 = !OverWriteRemoteFile && (RemoteFileName);
        if (flag2)
        {
          throw new Exception("The file with the same name already exists on the FTP service!");
        }
         = (new Uri(() + RemoteFileName), "STOR");
        Stream requestStream = ();
        MemoryStream memoryStream = new MemoryStream(FileBytes);
        byte[] array = new byte[1024];
        int num = 0;
        for (;;)
        {
          int num2 = (array, 0, );
          bool flag3 = num2 == 0;
          if (flag3)
          {
            break;
          }
          num += num2;
          (array, 0, num2);
        }
        ();
         = (FtpWebResponse)();
        ();
        ();
        FileBytes = null;
        result = true;
      }
      catch (Exception ex)
      {
         = ();
        throw ex;
      }
      return result;
    }

The above is the detailed content of the example of C# implementing FTP upload materials. For more information about C# ftp uploads, please follow my other related articles!