【问题标题】:Uploading files to ftp folder having special characters in name将文件上传到名称中包含特殊字符的 ftp 文件夹
【发布时间】:2017-01-03 06:54:55
【问题描述】:

我正在尝试在 FTP 文件夹上上传文件,但出现以下错误。

远程服务器返回错误:(550) 文件不可用(例如, 找不到文件,无法访问)

我正在使用以下示例对此进行测试:

    // 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();
  • 我可以上传父文件夹01-03-2017 上的文件,但不能上传目标文件夹ROLLINS, SETH S. M.D 中的文件,目标文件夹中显然有特殊字符。
  • 我可以使用 FileZilla 上传文件
  • 我尝试过HttpUtility.UrlEncode,但没有帮助

感谢您的时间和帮助。

【问题讨论】:

  • 问题出在网址上:在每个文件夹名称后使用“//”而不是“/”@Ankit

标签: c# .net ftp


【解决方案1】:

您需要对 URL 路径中的空格(可能还有逗号)进行编码,例如:

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】:

    使用这样的东西:

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

    或者您可以使用以下代码形成一个 Uri 并将其传递给 webrequest。

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

    【讨论】:

    • 这会给你ftp%3a%2f%2f96.31.95.118%3a2121%2f%2f01-03-2017%2f%2fROLLINS%2c+SETH+S.+M.D%2f%2f30542_3117.wav。这看起来不像是一个正确的方法(即使它是偶然的)。
    • @MartinPrikryl,是的,这个解决方案不正确。我的评论放错地方了,应该是在 Prabhu 对这个问题发表评论之后。
    【解决方案3】:

    该代码适用于 C# 控制台应用程序,但不适用于 Web Api Action。我无法找到原因。

    所以我使用了一个免费的库。

    发布来自here 可用示例之一的示例代码:

    所以我使用了通过Nuget 提供的FluentFtp 库。

    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 如此处所述。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      • 2012-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多