首頁 > 軟體

C#實現FTP上傳檔案的方法

2022-04-14 16:02:20

1.通過用FTP進行上傳檔案,首先要實現建立FTP連線,一般建立FTP連線,需要知道FTP設定有關的資訊。一般要在Bean中建立一個ServiceFileInfo.cs檔案進行記錄,一般需要FTP地址、登入使用者名稱和登入密碼。然後通過其他頁面進行存取讀取。程式碼樣式如下:

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.通過主方法讀取Bean檔案下面的的ServiceFileInfo.cs檔案的資訊,去實現建立FTP連線。這裡還需要清楚的知道你上傳檔案的路徑(Path)和檔名稱(FileName)。根據這些資訊主方法去呼叫寫著Bean中的另外一個ftpOperation.cs 檔案(這個.cs檔案中主要寫一些關於FTP的操作方法),進行FTP存取操作。

  • 主方法呼叫FTP操作程式碼
ExecutionResult exeRes = this.ftpOperation.UploadFile(textFilePath, txtUID, txtPWD, Path + "/" + FileName + ".txt");//.txt為檔案的字尾名
  •  Bean檔案中ftpOperation.cs檔案關於FTP操作的方法
public ExecutionResult UploadFile(string vIMSPath, string vUID, string vPassword, string vLocalPath)
        {
            ExecutionResult result = new ExecutionResult();
            result = connectState(vIMSPath, vUID, vPassword, vLocalPath);//呼叫下面程式碼方法
            if (result.Status)
            {
                File.Delete(vLocalPath);
            }
            return result;
        }

connectState()方法

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()方法
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("非法檔名或目錄名!");
                }
                bool flag2 = File.Exists(LocalFullPath);
                if (!flag2)
                {
                    throw new Exception("本地檔案不存在!");
                }
                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("非法檔名或目錄名!");
            }
            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("非法檔名!");
                }
                bool flag2 = !OverWriteRemoteFile && this.FileExist(RemoteFileName);
                if (flag2)
                {
                    throw new Exception("FTP服務上面已經存在同名檔案!");
                }
                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;
        }

到此這篇關於C#實現FTP上傳檔案的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com