本文介绍了在一个会话中将多个文件上载到FTp服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您好, 我需要将多个文件上传到ftp服务器,我试图使用Webrequest一次发送每个文件,问题是每次我发送我需要添加凭据的文件,这意味着我打开了一个新会话。我尝试了不同的方法,但它不起作用,低于我最新的。有谁知道如何优雅地做到这一点?我需要一次发送文件,所以我不能使用异步。有人可以帮我吗? private void envoiFTP( string table) { string path = @ D:\Temp \; // string [] files = Directory.GetFiles(path, @ * .xml); if (files!= null ) { foreach (字符串文件 个文件) { fi = new FileInfo(file); string fileName = fi.Name; string fileurl = path + @ / + fileName; string ftpFile = FtpServer + @ / + fileName; FtpWebRequest myRequest =(FtpWebRequest)FtpWebRequest.Create(ftpFile); myRequest.Credentials = new NetworkCredential(FtpUser,FtpPassword); myRequest.Method = WebRequestMethods.Ftp.UploadFile; myRequest.Timeout = 1000000 ; myRequest.UseBinary = true ; myRequest.KeepAlive = true ; myRequest.ContentLength = fi.Length; byte [] buffer = new byte [ 4097 ]; int bytes = 0 ; int total_bytes =( int )fi.Length; System.IO.FileStream fs = fi.OpenRead(); System.IO.Stream rs = myRequest.GetRequestStream(); while (total_bytes > 0 ) { bytes = fs.Read(buffer, 0 ,buffer.Length); rs.Write(buffer, 0 ,bytes); total_bytes = total_bytes - bytes; } fs.Close(); rs.Close(); FtpWebResponse uploadResponse =(FtpWebResponse)myRequest.GetResponse(); uploadResponse.Close(); } } } } 解决方案 Hello, I need to upload multiple files to an ftp server, i tried to send each file a time using Webrequest, the problem is that each time I send a file i need to add credentials , which means i open up a new session. I tried different approach but it didn't work, below my latest. Does anyone have an idea how to do it gracefully? I need to send a file a time, so i can't use asynchronous. can anyone help me please ?private void envoiFTP(string table) { string path = @"D:\Temp\"; // string[] files = Directory.GetFiles(path,@"*.xml"); if (files != null) { foreach (string file in files) { fi = new FileInfo(file); string fileName = fi.Name; string fileurl = path + @"/" + fileName; string ftpFile = FtpServer + @"/" + fileName; FtpWebRequest myRequest = (FtpWebRequest)FtpWebRequest.Create(ftpFile); myRequest.Credentials = new NetworkCredential(FtpUser, FtpPassword); myRequest.Method = WebRequestMethods.Ftp.UploadFile; myRequest.Timeout = 1000000; myRequest.UseBinary = true; myRequest.KeepAlive = true; myRequest.ContentLength = fi.Length; byte[] buffer = new byte[4097]; int bytes = 0; int total_bytes = (int)fi.Length; System.IO.FileStream fs = fi.OpenRead(); System.IO.Stream rs = myRequest.GetRequestStream(); while (total_bytes > 0) { bytes = fs.Read(buffer, 0, buffer.Length); rs.Write(buffer, 0, bytes); total_bytes = total_bytes - bytes; } fs.Close(); rs.Close(); FtpWebResponse uploadResponse = (FtpWebResponse)myRequest.GetResponse(); uploadResponse.Close(); } } } } 解决方案 这篇关于在一个会话中将多个文件上载到FTp服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-17 18:54