Downloading multiple files from FTP server

梦想的初衷 提交于 2019-12-20 06:09:09

问题


I've multiple files on a ftp server.I do not know the names of these files except that they are all. xml files. How do I programmatically download these files using .Net's FtpWebRequest?

Thanks.


回答1:


Most likely you'll have to issue a Dir command that lists out all the files, then go through each one downloading it.

Here is some info on getting a directory listing.

http://msdn.microsoft.com/en-us/library/ms229716.aspx




回答2:


Take a look at the ListDirectory function. It's the equivalent of the NLIST command in FTP.




回答3:


You'll probably want to use an existing library like this one rather than write your own.




回答4:


FtpWebRequest __request = (FtpWebRequest)FtpWebRequest.Create(__requestLocation);
__request.Method = WebRequestMethods.Ftp.ListDirectory;

var __response = (FtpWebResponse)__request.GetResponse();
                        using (StreamReader __directoryList = new StreamReader(__response.GetResponseStream())) {
                            string ___line = __directoryList.ReadLine();
                            while (___line != null) {
                                if (!String.IsNullOrEmpty(___line)) { __output.Add(___line); }
                                ___line = __directoryList.ReadLine();
                            }

                            break;
                        }

Getting the target file...

FtpWebRequest __request = null;
FtpWebResponse __response = null;
byte[] __fileBuffer = null;
byte[] __outputBuffer = null;

__request = (FtpWebRequest)FtpWebRequest.Create(__requestLocation);
__request.Method = WebRequestMethods.Ftp.DownloadFile;

__response = (FtpWebResponse)__request.GetResponse();

using (MemoryStream __outputStream = new MemoryStream()) {
   using (Stream __responseStream = __response.GetResponseStream()) {
      using (BufferedStream ___outputBuffer = new BufferedStream(__responseStream)) {
         __fileBuffer = new byte[BLOCKSIZE];
         int ___readCount = __responseStream.Read(__fileBuffer, 0, BLOCKSIZE);

         while (___readCount > 0) {
            __outputStream.Write(__fileBuffer, 0, ___readCount);
            ___readCount = __responseStream.Read(__fileBuffer, 0, BLOCKSIZE);
         }

         __outputStream.Position = 0;
         __outputBuffer = new byte[__outputStream.Length];
         //Truncate Buffer to only the specified bytes. Store into output buffer
         Array.Copy(__outputStream.GetBuffer(), __outputBuffer, __outputStream.Length);

         break;
      }
   }
}

try { __response.Close(); } catch { }
__request = null;
__response = null;

return __outputBuffer;

Ripped out of some other code I have, so it probably wont compile and run directly.




回答5:


I don't know if the FtpWebRequest is a strict requirement. If you can use a third party component following code would accomplish your task:

// create client, connect and log in 
Ftp client = new Ftp();
client.Connect("ftp.example.org");
client.Login("username", "password");

// download all files in the current directory which matches the "*.xml" mask 
// at the server to the 'c:\data' directory 
client.GetFiles("*.xml", @"c:\data", FtpBatchTransferOptions.Default);

client.Disconnect();

The code uses Rebex FTP which can be downloaded here.

Disclaimer: I'm involved in the development of this product.



来源:https://stackoverflow.com/questions/1890886/downloading-multiple-files-from-ftp-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!