繁体   English   中英

Directory.GetFiles 从 FTP 站点

[英]Directory.GetFiles from FTP site

我在我的 asp.net 应用程序中使用此方法将上传文件从本地目录传输到 azure 存储。 我现在想做同样的事情,但使用 FTP 文件夹中的文件。 我已经研究过FtpWebRequest ,但不确定它如何或是否可以与当前方法一起使用?

foreach (string strFile in Directory.GetFiles("myftpsite", "*.jpg"))
{
    blob = blobContainer.GetBlobReference(strFile);
    blob.UploadFile(strFile);                
}
    public string[] directoryListDetailed(string directory)
    {
        try
        {
            /* Create an FTP Request */
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory);
            /* Log in to the FTP Server with the User Name and Password Provided */
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            /* When in doubt, use these options */
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            /* Specify the Type of FTP Request */
            ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            /* Establish Return Communication with the FTP Server */
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            /* Establish Return Communication with the FTP Server */
            ftpStream = ftpResponse.GetResponseStream();
            /* Get the FTP Server's Response Stream */
            StreamReader ftpReader = new StreamReader(ftpStream);
            /* Store the Raw Response */
            string directoryRaw = null;
            /* Read Each Line of the Response and Append a Pipe to Each Line for Easy Parsing */
            try
            {

                string[] separator = { "", " " };
                while (ftpReader.Peek() != -1) 
                   {
                       bool flg = false; 

                     foreach (var word in ftpReader.ReadLine().Split (separator , StringSplitOptions.RemoveEmptyEntries))
                     {

                         if (flg == true)
                         { directoryRaw += word.ToString() + "|"; flg = false; }
                         if (word.ToString () == "<DIR>")
                            flg = true;

                     }
                   } 
            }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            /* Resource Cleanup */
            ftpReader.Close();
            ftpStream.Close();
            ftpResponse.Close();
            ftpRequest = null;
            /* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */
            try { string[] directoryList = directoryRaw.Split("|".ToCharArray()); return directoryList; }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        }
        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        /* Return an Empty string Array if an Exception Occurs */
        return new string[] { "" };
    }

要列出 FTP 文件夹中的所有文件,请参阅:http: //msdn.microsoft.com/en-us/library/ms229716.aspx

我不知道直接阅读它们的任何方法,所以我会将它们下载到本地计算机并将它们上传到您需要它们的任何地方。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM