C#實(shí)現(xiàn)FTP上傳文件的方法
1.通過(guò)用FTP進(jìn)行上傳文件,首先要實(shí)現(xiàn)建立FTP連接,一般建立FTP連接,需要知道FTP配置有關(guān)的信息。一般要在Bean中建立一個(gè)ServiceFileInfo.cs文件進(jìn)行記錄,一般需要FTP地址、登錄用戶名和登錄密碼。然后通過(guò)其他頁(yè)面進(jìn)行訪問(wèn)讀取。代碼樣式如下:
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.通過(guò)主方法讀取Bean文件下面的的ServiceFileInfo.cs文件的信息,去實(shí)現(xiàn)建立FTP連接。這里還需要清楚的知道你上傳文件的路徑(Path)和文件名稱(FileName)。根據(jù)這些信息主方法去調(diào)用寫(xiě)著B(niǎo)ean中的另外一個(gè)ftpOperation.cs 文件(這個(gè).cs文件中主要寫(xiě)一些關(guān)于FTP的操作方法),進(jìn)行FTP訪問(wèn)操作。
- 主方法調(diào)用FTP操作代碼
ExecutionResult exeRes = this.ftpOperation.UploadFile(textFilePath, txtUID, txtPWD, Path + "/" + FileName + ".txt");//.txt為文件的后綴名
- Bean文件中ftpOperation.cs文件關(guān)于FTP操作的方法
public ExecutionResult UploadFile(string vIMSPath, string vUID, string vPassword, string vLocalPath) { ExecutionResult result = new ExecutionResult(); result = connectState(vIMSPath, vUID, vPassword, vLocalPath);//調(diào)用下面代碼方法 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服務(wù)上面已經(jīng)存在同名文件!"); } 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; }
到此這篇關(guān)于C#實(shí)現(xiàn)FTP上傳文件的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
WinForm中comboBox控件數(shù)據(jù)綁定實(shí)現(xiàn)方法
這篇文章主要介紹了WinForm中comboBox控件數(shù)據(jù)綁定實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了WinForm實(shí)現(xiàn)comboBox控件數(shù)據(jù)綁定的常用方法與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05漢字轉(zhuǎn)拼音縮寫(xiě)示例代碼(Silverlight和.NET 將漢字轉(zhuǎn)換成為拼音)
本篇文章主要介紹了漢字轉(zhuǎn)拼音縮寫(xiě)示例代碼(Silverlight和.NET 將漢字轉(zhuǎn)換成為拼音) 需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01C#實(shí)現(xiàn)微信退款及對(duì)賬功能的示例詳解
在招聘報(bào)名系統(tǒng)里,考務(wù)費(fèi)支付是其中一個(gè)環(huán)節(jié),支付方式很多種,比如銀聯(lián)、微信、支付寶等等,本次我們以微信支付進(jìn)行舉例,在實(shí)際的應(yīng)用中,對(duì)于支付成功的考生,我們會(huì)遇到實(shí)現(xiàn)退款的需求,所以本文給大家介紹了使用C#實(shí)現(xiàn)微信退款及對(duì)賬,需要的朋友可以參考下2023-11-11C#查找素?cái)?shù)實(shí)現(xiàn)方法
這篇文章主要介紹了C#查找素?cái)?shù)實(shí)現(xiàn)方法,程序中有很多使用的功能模塊,非常適合C#初學(xué)者學(xué)習(xí)借鑒,需要的朋友可以參考下2014-08-08C#中FormClosing與FormClosed的區(qū)別詳細(xì)解析
本文是對(duì)C#中FormClosing與FormClosed的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-10-10C# winform打開(kāi)Excel文檔的方法總結(jié)(必看篇)
下面小編就為大家?guī)?lái)一篇C# winform打開(kāi)Excel文檔的方法總結(jié)(必看篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01C#實(shí)現(xiàn)對(duì)象XML序列化的方法
這篇文章主要介紹了C#實(shí)現(xiàn)對(duì)象XML序列化的方法,是C#常見(jiàn)的實(shí)用技巧,需要的朋友可以參考下2014-11-11