2012-11-07 3 views
-3

FTP를 통해 폴더 (및 해당 콘텐츠)를 업로드하려고하는데 파일을 시도 할 때 파일이 선택되지 않는다는 오류가 계속 나타납니다.FTP를 통해 폴더를 업로드하는 방법은 무엇입니까?

폴더와 콘텐츠를 업로드 할 수 있도록 만드는 방법을 알고 있습니까?

다음은 현재 사용중인 코드입니다.

public void UploadFile(string _FileName, string _UploadPath, string _FTPUser, string _FTPPass) 
{ 
    System.IO.FileInfo _FileInfo = new System.IO.FileInfo(_FileName); 

    // Create FtpWebRequest object from the Uri provided 
    System.Net.FtpWebRequest _FtpWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(new Uri(_UploadPath)); 

    // Provide the WebPermission Credintials 
    _FtpWebRequest.Credentials = new System.Net.NetworkCredential(_FTPUser, _FTPPass); 

    // By default KeepAlive is true, where the control connection is not closed 
    // after a command is executed. 
    _FtpWebRequest.KeepAlive = false; 

    // set timeout for 20 seconds 
    _FtpWebRequest.Timeout = 20000; 

    // Specify the command to be executed. 
    _FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile; 

    // Specify the data transfer type. 
    _FtpWebRequest.UseBinary = true; 

    // Notify the server about the size of the uploaded file 
    _FtpWebRequest.ContentLength = _FileInfo.Length; 

    // The buffer size is set to 2kb 
    int buffLength = 2048; 
    byte[] buff = new byte[buffLength]; 

    // Opens a file stream (System.IO.FileStream) to read the file to be uploaded 
    System.IO.FileStream _FileStream = _FileInfo.OpenRead(); 

    try 
    { 
     // Stream to which the file to be upload is written 
     System.IO.Stream _Stream = _FtpWebRequest.GetRequestStream(); 

     // Read from the file stream 2kb at a time 
     int contentLen = _FileStream.Read(buff, 0, buffLength); 

     // Till Stream content ends 
     while (contentLen != 0) 
     { 
      // Write Content from the file stream to the FTP Upload Stream 
      _Stream.Write(buff, 0, contentLen); 
      contentLen = _FileStream.Read(buff, 0, buffLength); 
     } 

     // Close the file stream and the Request Stream 
     _Stream.Close(); 
     _Stream.Dispose(); 
     _FileStream.Close(); 
     _FileStream.Dispose(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 
} 

답변

1

귀하의 URI가 잘못된 것입니다, 당신은에 파일 이름을 지정해야합니다 : 이것은 내가 사용하고있는 서브가

UploadFile(@"C:\\MainPlugins\\Plugins", "ftp://testsiteurl.com/public_html/wp-content/plugins/test plugin", "username", "password"); 

: (I 폴더 플러그인의 모든 파일을 업로드 할) URI.

// Create FtpWebRequest object from the Uri provided 
    System.Net.FtpWebRequest _FtpWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(new Uri(_UploadPath + "/" + _FileInfo.Name)); 

또한 예외가 발생했을 때 스트림을 적절히 처리하려면 '사용'키워드를 사용하는 것이 좋습니다.

using (System.IO.FileStream _FileStream = _FileInfo.OpenRead()) 
       { 

        // Stream to which the file to be upload is written 

        using (System.IO.Stream _Stream = _FtpWebRequest.GetRequestStream()) 
        { 
         // Read from the file stream 2kb at a time 
         int contentLen = _FileStream.Read(buff, 0, buffLength); 

         // Till Stream content ends 
         while (contentLen != 0) 
         { 
          // Write Content from the file stream to the FTP Upload Stream 
          _Stream.Write(buff, 0, contentLen); 
          contentLen = _FileStream.Read(buff, 0, buffLength); 
         } 

         //No need to Close the file stream and the Request Stream 

        } 
       } 
+0

작성한 코드의 첫 번째 줄은 어디에 넣어야합니까? 하위 UploadFile을 호출하지 마십시오. –

+0

여전히 폴더를 업로드 할 수 없습니다. –

+0

UploadFile 메서드에서 코드를 편집했습니다. 폴더를 업로드하려면 폴더의 파일을 루프로 반복하고 미리 Ftp.MakeDirectory 메서드를 사용해야합니다. 어느 쪽이든이 방법은 아마도 파일을 업로드하는 방법이 아닙니다. 비동기로 메소드를 수정해야합니다. – Mataniko

관련 문제