2011-11-04 84 views
0

我在我的asp.net應用程序中使用此方法將上傳文件從本地目錄傳輸到天藍色存儲。我現在想要做同樣的事情,但使用FTP文件夾中的文件。我已經看過FtpWebRequest,但不知道如何或如果它能用這種當前的方法?來自FTP站點的Directory.GetFiles

foreach (string strFile in Directory.GetFiles("myftpsite", "*.jpg")) 
{ 
    blob = blobContainer.GetBlobReference(strFile); 
    blob.UploadFile(strFile);     
} 
+0

您是否打算將它們從blob容器發送到遠程位置上的物理文件? –

+0

不,它從FTP文件夾到blob容器 –

+2

您不能直接從FTP到BLOB。在某個地方需要有一箇中間人。 –

回答

0

上市的FTP文件夾中看到所有文件:http://msdn.microsoft.com/en-us/library/ms229716.aspx

我不知道任何方式直接讀取他們,所以我想將它們下載到本地計算機,並將其上傳到哪裏你需要他們。

+0

嗨,文件必須直接從FTP到blob,不涉及本地機器。 –

+0

那麼你可以肯定的是你正在執行代碼的RAM。那麼可能暫時將文件存儲在那裏?當然,這不適用於非常大的文件。 – Sylence

0
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[] { "" }; 
    } 
+3

-1您錯過了將Stream和StreamReader放入'using'塊。 –