C ා realize FTP upload data

C ා realize FTP upload data

1. By using FTP to upload files, the first step is to establish an FTP connection. Generally, to establish an FTP connection, you need to know the information about the FTP configuration. Generally, you need to create a ServiceFileInfo.cs File for recording, generally requires FTP address, login user name and login password. Then 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 ServiceFileInfo.cs File information to establish FTP connection. You also need to know the path and filename of the file you uploaded. According to this information, the main method calls another Bean ftpOperation.cs File (this. CS file mainly writes some FTP operation methods) for FTP access operation.

  • Main method call FTP operation code
ExecutionResult exeRes = this.ftpOperation.UploadFile(textFilePath, txtUID, txtPWD, Path + "/" + FileName + ".txt");//.txt Is the suffix of the file
  • In Bean file ftpOperation.cs File about FTP operation method
 public ExecutionResult UploadFile(string vIMSPath, string vUID, string vPassword, string vLocalPath)
        {
            ExecutionResult result = new ExecutionResult();
            result = connectState(vIMSPath, vUID, vPassword, vLocalPath);//Call the following code method
            if (result.Status)
            {
                File.Delete(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 = ftp.UploadFile(fileName, Path.GetFileName(fileName), true);
                    if (Flag)
                    {
                        result.Status = true;
                        result.Message = "Send to server OK";
                    }
                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Anything = "Mail";
                    result.Message = operater + ":" + ex.Message;
                }
            }
            return result;
        }
  • UploadFile() method
public bool UploadFile(string LocalFullPath, string RemoteFileName, bool OverWriteRemoteFile)
        {
            bool result;
            try
            {
                bool flag = !this.IsValidFileChars(RemoteFileName) || !this.IsValidFileChars(Path.GetFileName(LocalFullPath)) || !this.IsValidPathChars(Path.GetDirectoryName(LocalFullPath));
                if (flag)
                {
                    throw new Exception("Illegal filename or directory name!");
                }
                bool flag2 = File.Exists(LocalFullPath);
                if (!flag2)
                {
                    throw new Exception("Local file does not exist!");
                }
                FileStream fileStream = new FileStream(LocalFullPath, FileMode.Open, FileAccess.Read);
                byte[] array = new byte[fileStream.Length];
                fileStream.Read(array, 0, (int)fileStream.Length);
                fileStream.Close();
                result = this.UploadFile(array, RemoteFileName, OverWriteRemoteFile);
            }
            catch (Exception ex)
            {
                this.ErrorMsg = ex.ToString();
                throw ex;
            }
            return result;
        }



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


public bool UploadFile(byte[] FileBytes, string RemoteFileName, bool OverWriteRemoteFile)
        {
            bool result;
            try
            {
                bool flag = !this.IsValidFileChars(RemoteFileName);
                if (flag)
                {
                    throw new Exception("Illegal filename!");
                }
                bool flag2 = !OverWriteRemoteFile && this.FileExist(RemoteFileName);
                if (flag2)
                {
                    throw new Exception("FTP A file with the same name already exists on the service!");
                }
                this.Response = this.Open(new Uri(this.Uri.ToString() + RemoteFileName), "STOR");
                Stream requestStream = this.Request.GetRequestStream();
                MemoryStream memoryStream = new MemoryStream(FileBytes);
                byte[] array = new byte[1024];
                int num = 0;
                for (;;)
                {
                    int num2 = memoryStream.Read(array, 0, array.Length);
                    bool flag3 = num2 == 0;
                    if (flag3)
                    {
                        break;
                    }
                    num += num2;
                    requestStream.Write(array, 0, num2);
                }
                requestStream.Close();
                this.Response = (FtpWebResponse)this.Request.GetResponse();
                memoryStream.Close();
                memoryStream.Dispose();
                FileBytes = null;
                result = true;
            }
            catch (Exception ex)
            {
                this.ErrorMsg = ex.ToString();
                throw ex;
            }
            return result;
        }

Tags: C# ftp

Posted on Wed, 13 May 2020 07:36:01 -0700 by alefort