2017-01-03 11 views
1

FTPフォルダにファイルをアップロードしようとしていますが、次のエラーが表示されます。私はこれをテストするには、次のサンプルを使用しています名前に特殊文字を含むftpフォルダにファイルをアップロードする

The remote server returned an error: (550) File unavailable (e.g., file not found, no access)

私はではなく、ターゲットフォルダの親フォルダ 01-03-2017上のファイルをアップロードすることができる午前
// Get the object used to communicate with the server. 
    string path = HttpUtility.UrlEncode("ftp://host:port//01-03-2017/John, Doe S. M.D/file.wav"); 
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(path); 
    request.Method = WebRequestMethods.Ftp.UploadFile; 

    // This example assumes the FTP site uses anonymous logon. 
    request.Credentials = new NetworkCredential("user", "password"); 

    // Copy the contents of the file to the request stream. 
    StreamReader sourceStream = new StreamReader(@"localpath\example.wav"); 
    byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); 
    sourceStream.Close(); 
    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(); 
  • 明確に特殊文字を持っていROLLINS, SETH S. M.Dそれ。
  • 私はHttpUtility.UrlEncodeに試してみましたが、それはあなたの時間と助けを

感謝を助けていないFileZillaの

  • を使用してファイルをアップロードすることができています。

  • +0

    問題はURLにあります:「/」の代わりにすべてのフォルダ名の後に「@」を使用してください@Ankit – Prabu

    答えて

    2

    使用このような何か:

    string path = HttpUtility.UrlEncode("ftp://96.31.95.118:2121//01-03-2017//ROLLINS, SETH S. M.D//30542_3117.wav"); 
    

    またはあなたは、次のコードを使用してURIを形成し、WebRequestクラスにそれを渡すことができます。

    あなたが好き、URLパスにスペース(多分コンマ)をエンコードする必要があり
    var path = new Uri("ftp://96.31.95.118:2121//01-03-2017//ROLLINS, SETH S. M.D//30542_3117.wav"); 
    
    +0

    これはあなたに 'ftp%3a%2f%2f96.31.95.118%3a2121%2f%2f01- 03-2017%2f%2fROLLINS%2c + SETH + S. + MD%2f%2f30542_3117.wav'。それは正しい方法のようには見えません(たとえそれが偶然に働くとしても)。 –

    +0

    @MartinPrikryl、はい、この解決策は間違っています。私のコメントは間違っていて、質問に対するPrabhuのコメントの後であったはずです。 – Ankit

    2

    string path = 
        "ftp://host:port/01-03-2017/" + 
        HttpUtility.UrlEncode("John, Doe S. M.D") + "/file.wav"; 
    

    効果的に、あなたが得る:

    ftp://host:port/01-03-2017/John%2c+Doe+S.+M.D/file.wav 
    
    2

    をコードは、C#で動作しますコンソールアプリケーションでは動作しませんでしたが、Web API動作では機能しませんでした。私はその理由を見つけることができなかった。

    私は同じライブラリを無料で使用しています。

    here入手可能な例の1からのサンプルコードを投稿:

    だから私はNugetを通じて利用可能FluentFtp libaryを使用していました。

    using System; 
    using System.IO; 
    using System.Net; 
    using FluentFTP; 
    
    namespace Examples { 
        public class OpenWriteExample { 
         public static void OpenWrite() { 
          using (FtpClient conn = new FtpClient()) { 
           conn.Host = "localhost"; 
           conn.Credentials = new NetworkCredential("ftptest", "ftptest"); 
    
           using (Stream ostream = conn.OpenWrite("01-03-2017/John, Doe S. M.D/file.wav")) { 
            try { 
             // istream.Position is incremented accordingly to the writes you perform 
            } 
            finally { 
             ostream.Close(); 
            } 
           } 
          } 
         } 
        } 
    } 
    

    また、ファイルがバイナリファイルの場合、ここで説明したようにStreamReader should not be usedです。

    関連する問題