0

我需要将多个文件上传到 ftp 服务器,我尝试使用 Webrequest 一次发送每个文件,问题是每次发送文件时我都需要添加凭据,这意味着我打开了一个新会话。我尝试了不同的方法,但没有奏效,低于我的最新方法。有谁知道如何优雅地做到这一点?我需要使用一个 FTPWeb 请求一次发送多个文件,每次发送文件时都没有连接,请考虑这些文件非常庞大。谁能帮帮我?

 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();
                }


           } 
                }


            }


    }
4

0 回答 0