c#實現FTP上傳

來源:互聯網
上載者:User

標籤:

  1         /// <summary>  2         /// 上傳檔案  3         /// </summary>  4         /// <param name="fileinfo">需要上傳的檔案</param>  5         /// <param name="targetDir">目標路徑</param>  6         /// <param name="hostname">ftp地址</param>  7         /// <param name="username">ftp使用者名稱</param>  8         /// <param name="password">ftp密碼</param>  9         public static void UploadFile(FileInfo fileinfo, string targetDir, string hostname, string username, string password) 10         { 11             //1. check target 12             string target; 13             if (targetDir.Trim() == "") 14             { 15                 return; 16             } 17             target = Guid.NewGuid().ToString();  //使用臨時檔案名稱 18  19  20             string URI = "FTP://" + hostname + "/" + targetDir + "/" + target; 21             ///WebClient webcl = new WebClient(); 22             System.Net.FtpWebRequest ftp = GetRequest(URI, username, password); 23  24             //設定FTP命令 設定所要執行的FTP命令, 25             //ftp.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails;//假設此處為顯示指定路徑下的檔案清單 26             ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile; 27             //指定檔案傳輸的資料類型 28             ftp.UseBinary = true; 29             ftp.UsePassive = true; 30  31             //告訴ftp檔案大小 32             ftp.ContentLength = fileinfo.Length; 33             //緩衝大小設定為2KB 34             const int BufferSize = 2048; 35             byte[] content = new byte[BufferSize - 1 + 1]; 36             int dataRead; 37  38             //開啟一個檔案流 (System.IO.FileStream) 去讀上傳的檔案 39             using (FileStream fs = fileinfo.OpenRead()) 40             { 41                 try 42                 { 43                     //把上傳的檔案寫入流 44                     using (Stream rs = ftp.GetRequestStream()) 45                     { 46                         do 47                         { 48                             //每次讀檔案流的2KB 49                             dataRead = fs.Read(content, 0, BufferSize); 50                             rs.Write(content, 0, dataRead); 51                         } while (!(dataRead < BufferSize)); 52                         rs.Close(); 53                     } 54  55                 } 56                 catch (Exception ex) { } 57                 finally 58                 { 59                     fs.Close(); 60                 } 61  62             } 63  64             ftp = null; 65             //設定FTP命令 66             ftp = GetRequest(URI, username, password); 67             ftp.Method = System.Net.WebRequestMethods.Ftp.Rename; //改名 68             ftp.RenameTo = fileinfo.Name; 69             try 70             { 71                 ftp.GetResponse(); 72             } 73             catch (Exception ex) 74             { 75                 ftp = GetRequest(URI, username, password); 76                 ftp.Method = System.Net.WebRequestMethods.Ftp.DeleteFile; //刪除 77                 ftp.GetResponse(); 78                 throw ex; 79             } 80             finally 81             { 82                 //fileinfo.Delete(); 83             } 84  85             // 可以記錄一個日誌  "上傳" + fileinfo.FullName + "上傳到" + "FTP://" + hostname + "/" + targetDir + "/" + fileinfo.Name + "成功." ); 86             ftp = null; 87  88             #region 89             /***** 90              *FtpWebResponse 91              * ****/ 92             //FtpWebResponse ftpWebResponse = (FtpWebResponse)ftp.GetResponse(); 93             #endregion 94         } 95  96         private static FtpWebRequest GetRequest(string URI, string username, string password) 97         { 98             //根據伺服器資訊FtpWebRequest建立類的對象 99             FtpWebRequest result = (FtpWebRequest)FtpWebRequest.Create(URI);100             //提供身分識別驗證資訊101             result.Credentials = new System.Net.NetworkCredential(username, password);102             //佈建要求完成之後是否保持到FTP伺服器的控制串連,預設值為true103             result.KeepAlive = false;104             return result;105         }

 

c#實現FTP上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.