FTP ด้วย WebRequest มีปัญหากับไฟล์ PDF แต่ถ้าใช้ไลบรารี WinSCP ไม่มีปัญหา
- FTP – Download Files
- FTP – Upload Files
- FTP – List Directory Contents
- FTP – Create Directory
1.FTP – Download Files
using System; using System.IO; using System.Net; namespace Examples.System.Net { public class WebRequestGetExample { public static void Main () { // Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm"); request.Method = WebRequestMethods.Ftp.DownloadFile; // This example assumes the FTP site uses anonymous logon. request.Credentials = new NetworkCredential("anonymous","janeDoe@contoso.com"); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); Console.WriteLine(reader.ReadToEnd()); Console.WriteLine($"Download Complete, status {response.StatusDescription}"); reader.Close(); response.Close(); } } }
หรืออ่านค่าจาก config แบบนี้
using System; using System.Configuration; using System.IO; using System.Net; namespace ConsoleApp1 { class Program { static void Main(string[] args) { string FTPServer = ConfigurationManager.AppSettings.Get("FTPServer"); string FTPUserName = ConfigurationManager.AppSettings.Get("FTPUserName"); string FTPPassword = ConfigurationManager.AppSettings.Get("FTPPassword"); string filename = "/test.txt"; string downloadUrl = string.Format("ftp://{0}//{1}", FTPServer, filename); FtpWebRequest request = (FtpWebRequest)WebRequest.Create(downloadUrl); request.Method = WebRequestMethods.Ftp.DownloadFile; request.Credentials = new NetworkCredential(FTPUserName, FTPPassword); using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(responseStream)) { Console.WriteLine(reader.ReadToEnd()); } } } } } }
เมื่อรันจะอ่านข้อความในไฟล์ที่กำหนด ออกมาทางหน้าจอ
หรือถ้าจะ save เป็นไฟล์ ให้เปลี่ยนบรรทัดที่ 30-33 เป็น
string downloadfileName = "x.txt"; using (FileStream writeStream = new FileStream(downloadfileName, FileMode.Create)) { int Length = 2048; Byte[] buffer = new Byte[Length]; int bytesRead = responseStream.Read(buffer, 0, Length); while (bytesRead > 0) { writeStream.Write(buffer, 0, bytesRead); bytesRead = responseStream.Read(buffer, 0, Length); } }
2.FTP – Upload Files
using System; using System.IO; using System.Net; using System.Text; namespace Examples.System.Net { public class WebRequestGetExample { public static void Main () { // Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm"); request.Method = WebRequestMethods.Ftp.UploadFile; // This example assumes the FTP site uses anonymous logon. request.Credentials = new NetworkCredential("anonymous", "janeDoe@contoso.com"); // Copy the contents of the file to the request stream. byte[] fileContents; using (StreamReader sourceStream = new StreamReader("testfile.txt")) { fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); } request.ContentLength = fileContents.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(fileContents, 0, fileContents.Length); } using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) { Console.WriteLine($"Upload File Complete, status {response.StatusDescription}"); } } } }
3.FTP – List Directory Contents
using System; using System.IO; using System.Net; namespace Examples.System.Net { public class WebRequestGetExample { public static void Main () { // Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/"); request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; // This example assumes the FTP site uses anonymous logon. request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com"); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); Console.WriteLine(reader.ReadToEnd()); Console.WriteLine($"Directory List Complete, status {response.StatusDescription}"); reader.Close(); response.Close(); } } }
4.FTP – Create Directory
public static bool CreateDirectory(string remoteFolder) { try { log.Info(string.Format("Parameter ('{0}')", remoteFolder)); // FTP server config string FTPServer = ConfigurationManager.AppSettings.Get("FTPServer"); string FTPUserName = ConfigurationManager.AppSettings.Get("FTPUserName"); string FTPPassword = ConfigurationManager.AppSettings.Get("FTPPassword"); WebRequest request = WebRequest.Create(string.Format("ftp://{0}/{1}", FTPServer, remoteFolder)); request.Method = WebRequestMethods.Ftp.MakeDirectory; request.Credentials = new NetworkCredential(FTPUserName, FTPPassword); using (var resp = (FtpWebResponse)request.GetResponse()) { log.Info(resp.StatusCode); } return true; } catch (Exception e) { log.Error(string.Format("Error: {0}", e.Message)); return false; } }