2014-07-25 30 views
0

我得到的錯誤是web異常。所請求的URI對於此FTP命令無效。FTP流請求流問題

我不確定如何解決它。任何人有任何想法?謝謝

private void SendFile(FileInfo file) 
     { 
      Console.WriteLine("ftp://" + ipAddressTextField.Text); 
      // Get the object used to communicate with the server. 
      string ftp = "ftp://" + ipAddressTextField.Text; 
      FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp); 
      request.Method = WebRequestMethods.Ftp.UploadFile; 

      // This example assumes the FTP site uses anonymous logon. 
      request.Credentials = new NetworkCredential(usernameTextField.Text, passwordTextField.Text); 


      // Copy the contents of the file to the request stream. 
      byte[] fileContents = File.ReadAllBytes(file.FullName); 
      request.ContentLength = fileContents.Length; 

      Stream requestStream = request.GetRequestStream(); 
      requestStream.Write(fileContents, 0, fileContents.Length); 
      requestStream.Close(); 

      FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 

      Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription); 

      response.Close(); 
     } 

回答

1

似乎你沒有設置上傳的目標文件名,所以服務器沒有文件名用於上傳的文件。

你可以使用類似的東西來設置它與源文件名相同;

string ftp = "ftp://" + ipAddressTextField.Text + "/" + file.Name; 
Console.WriteLine(ftp); 

做一個稍微更強大的創作URI的,該文件名可能包含特殊字符,你可以使用Uri類來構建它,並通過在對WebRequest.Create代替的情況。