private static FtpClient createFtpClient() {\n\tFtpClient ftp = new FtpClient(ftpAddress, ftpPort);\n\tftp.Credentials = new System.Net.NetworkCredential(ftpAcc, ftpPwd);\n\n\tftp.Connect();\n\treturn ftp;\n}<\/code><\/pre><\/div>\n\n\n\n
Here is a simple way to establish a shared connection. Since there are different operations on FTP in the project, the connection method is extracted and written independently.<\/p>\n\n\n\n
Connection details should include:<\/p>\n\n\n\n
\n- ftpAddress: IP address of the FTP server (aliases seem to work as well).<\/li>\n\n\n\n
- ftpPort: Port number of the FTP server.<\/li>\n\n\n\n
- ftpAcc, ftpPwd: User account and password for FTP connection.<\/li>\n<\/ul>\n\n\n\n
Additionally, since there is a Connect <\/em><\/strong>method, there is also a Disconnect<\/em><\/strong>. Typically, it is called after usage to terminate the connection.<\/p>\n\n\n\n<\/div>\n\n\n\n
<\/span>Creating a Folder<\/strong><\/span><\/h3>\n\n\n\npublic static bool initDict() {\n\tbool result = false;\n\ttry\n\t{\n\t\tFtpClient ftp = createFtpClient();\n\t\tresult = ftp.CreateDirectory("pdf");\n\n\t\tftp.Disconnect();\n\t}\n\tcatch (Exception ex) {\n\t\tthrow ex;\n\t}\n\n\treturn result;\n}<\/code><\/pre><\/div>\n\n\n\nftp.CreateDirectory<\/em><\/strong> : If the specified directory doesn’t exist, it will be created<\/p>\n\n\n\nThis can also assist in creating multiple levels. For example, ftp.CreateDirectory(“pdf\\\\test”)<\/em><\/strong> will create a ‘pdf<\/strong>‘ folder at the root, and underneath it, a subfolder named ‘test<\/strong>‘.<\/p>\n\n\n\n<\/div>\n\n\n\n
<\/span>Upload (Single File)<\/strong><\/span><\/h3>\n\n\n\npublic static bool uploadPdf(HttpPostedFileBase file) {\n\tbool result = false;\n\tFtpClient ftp = createFtpClient();\n\t\n\tbyte[] fileBytes;\n\tusing (BinaryReader reader = new BinaryReader(file.InputStream))\n\t{\n\t\tfileBytes = reader.ReadBytes(file.ContentLength);\n\t}\n\n\tFtpStatus ftpStatus = ftp.UploadBytes(fileBytes, "pdf\\\\" + file.FileName);\n\n\tftp.Disconnect();\n\treturn result;\n}<\/code><\/pre><\/div>\n\n\n\nIn this section, the UploadBytes <\/em><\/strong>method is utilized to upload a byte array. The provided example involves transmitting a PDF file to the “pdf<\/strong>” folder on the FTP server.<\/p>\n\n\n\nFurthermore, FtpStatus has three possible values: 0 for Failed, 1 for Success, and 2 for Skipped. The “Skipped” status occurs when the file already exists on the FTP server.<\/p>\n\n\n\n
<\/div>\n\n\n\n
<\/span>Upload an entire directory to an FTP directory<\/span><\/h3>\n\n\n\npublic static void uploadDir(string uploadDir, string desDir) {\n\tFtpClient ftp = createFtpClient();\n\n\tftp.CreateDirectory(desDir);\n\tList<FtpResult> res = ftp.UploadDirectory(uploadDir, desDir);\n\tftp.Disconnect();\n}<\/code><\/pre><\/div>\n\n\n\nftp.UploadDirectory <\/em><\/strong>: Uploads the entire local directory to the specified directory on the FTP server.<\/p>\n\n\n\nThe method returns a List of FtpResult objects, which contains the status of each file in the upload process.<\/p>\n\n\n\n
<\/div>\n\n\n\n
<\/span>Download Single File<\/strong><\/span><\/h3>\n\n\n\npublic static byte[] downloadPdf(string pdfName) {\n\tbyte[] arr;\n\tFtpClient ftp = createFtpClient();\n\n\tbool res = ftp.DownloadBytes(out arr, "pdf\\\\" + pdfName);\n\tftp.Disconnect();\n\n\treturn arr;\n}<\/code><\/pre><\/div>\n\n\n\nDownload a file using the out parameter, providing the byte[] and specifying the file location. <\/p>\n\n\n\n
In this example, it downloads the previously uploaded PDF.<\/p>\n\n\n\n
<\/div>\n\n\n\n
<\/span>List the current directory<\/strong><\/span><\/h3>\n\n\n\n\/\/\/ <summary>\n\/\/\/ List the contents of the current directory (non-recursive).\n\/\/\/ <\/summary>\npublic void ListAll()\n{\n FtpClient ftp = CreateFtpClient();\n\n foreach (var item in ftp.GetListing())\n {\n Debug.WriteLine(item.ToString());\n }\n\n ftp.Disconnect();\n}<\/code><\/pre><\/div>\n\n\n\n<\/div>\n\n\n\n