using FluentFTP; using System; using System.IO; using System.IO.Compression; using System.Net; using System.Text; using System.Threading.Tasks; namespace TeamAAS_VP.Core { public class FtpUploaderTest { public FtpUploaderTest() { } public async void UploadFileTest() { //var rootPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CuttingLine"); //string sourcePath = "Path.Combine(rootPath, SelectedFile)"; string sourcePath = @"C:\Users\Administrator\Desktop\test.txt"; string content = File.ReadAllText(sourcePath); string ftpHost = "192.168.0.40"; string ftpUserName = "default"; string ftpPwd = "default"; content += "END"; var byteArray = Encoding.ASCII.GetBytes(content); // FluentFTP async upload (replaces deprecated FtpWebRequest) using (var ftp = new FtpClient(ftpHost, new NetworkCredential(ftpUserName, ftpPwd))) { ftp.Config.EncryptionMode = FtpEncryptionMode.None; // plain FTP ftp.Config.DataConnectionType = FtpDataConnectionType.PASV; // passive mode ftp.Connect(); var remotePath = "/usr/usrapp/saveCAM/data.txt"; using (var ms = new MemoryStream(byteArray)) { ftp.UploadStream( ms, remotePath, FtpRemoteExists.Overwrite, createRemoteDir: true ); await Task.Delay(100).ConfigureAwait(false); Console.WriteLine("File uploaded successfully."); } } } // 测试上传ZIP文件,保持内容不变 public async Task UploadZipFileTest() { string sourcePath = @"C:\Users\Administrator\Desktop\test.zip"; // ZIP文件路径 string ftpHost = "192.168.0.40"; string ftpUserName = "default"; string ftpPwd = "default"; // 读取文件字节(不修改内容) byte[] fileBytes = File.ReadAllBytes(sourcePath); using (var ftp = new FtpClient(ftpHost, new NetworkCredential(ftpUserName, ftpPwd))) { ftp.Config.EncryptionMode = FtpEncryptionMode.None; ftp.Config.DataConnectionType = FtpDataConnectionType.PASV; ftp.Connect(); var remotePath = "/usr/usrapp/saveCAM/data.zip"; // 远程路径 using (var ms = new MemoryStream(fileBytes)) { ftp.UploadStream( ms, remotePath, FtpRemoteExists.Overwrite, createRemoteDir: true ); Console.WriteLine("ZIP file uploaded successfully."); } } } // 测试上传整个文件夹,保持结构不变 public async Task UploadFolderTest() { string localFolderPath = @"C:\Users\Administrator\Desktop\MyFolder"; // 本地文件夹 string ftpHost = "192.168.0.40"; string ftpUserName = "default"; string ftpPwd = "default"; string remoteBasePath = "/usr/usrapp/saveCAM/MyFolder"; // 远程基础路径 using (var ftp = new FtpClient(ftpHost, new NetworkCredential(ftpUserName, ftpPwd))) { ftp.Config.EncryptionMode = FtpEncryptionMode.None; ftp.Config.DataConnectionType = FtpDataConnectionType.PASV; ftp.Connect(); await UploadDirectoryRecursively(ftp, localFolderPath, remoteBasePath); Console.WriteLine("Folder uploaded successfully."); } } private async Task UploadDirectoryRecursively(FtpClient ftp, string localPath, string remotePath) { // 创建远程目录 if (!ftp.DirectoryExists(remotePath)) { ftp.CreateDirectory(remotePath); } // 上传文件 foreach (string file in Directory.GetFiles(localPath)) { string fileName = Path.GetFileName(file); string remoteFilePath = $"{remotePath}/{fileName}".Replace("\\", "/"); byte[] fileBytes = File.ReadAllBytes(file); using (var ms = new MemoryStream(fileBytes)) { ftp.UploadStream( ms, remoteFilePath, FtpRemoteExists.Overwrite, createRemoteDir: false ); } Console.WriteLine($"Uploaded file: {fileName}"); } // 递归处理子文件夹 foreach (string subDir in Directory.GetDirectories(localPath)) { string dirName = new DirectoryInfo(subDir).Name; string remoteSubPath = $"{remotePath}/{dirName}".Replace("\\", "/"); await UploadDirectoryRecursively(ftp, subDir, remoteSubPath); } await Task.CompletedTask; } // 测试上传整个文件夹作为ZIP,保持内容不变 public async Task UploadFolderAsZipTest() { string sourceFolder = @"D:\image\Recored\2026-03\03-02\产品1\CN0D0NDYCKJ006250103X02"; string tempZipPath = Path.Combine(Path.GetTempPath(), $"CN0D0NDYCKJ006250103X02.zip"); try { // 创建ZIP文件 ZipFile.CreateFromDirectory(sourceFolder, tempZipPath); // 上传ZIP string ftpHost = "10.130.63.3"; string ftpUserName = "AE"; string ftpPwd = "LUX#PCiCT2026"; byte[] zipBytes = File.ReadAllBytes(tempZipPath); using (var ftp = new FtpClient(ftpHost, new NetworkCredential(ftpUserName, ftpPwd))) { ftp.Config.EncryptionMode = FtpEncryptionMode.None; ftp.Config.DataConnectionType = FtpDataConnectionType.PASV; ftp.Config.ConnectTimeout = 15000; ftp.Connect(); var remotePath = $"/ae/KB_Screw_1/CN0D0NDYCKJ006250103X02.zip"; using (var ms = new MemoryStream(zipBytes)) { ftp.UploadStream( ms, remotePath, FtpRemoteExists.Overwrite, createRemoteDir: true ); } Console.WriteLine("Folder compressed and uploaded as ZIP successfully."); } } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } finally { // 清理临时文件 if (File.Exists(tempZipPath)) { File.Delete(tempZipPath); } } } } }