【问题标题】:Why when uploading file format 20%.jpg i'm getting exception?为什么上传文件格式 20%.jpg 时出现异常?
【发布时间】:2014-12-16 11:17:35
【问题描述】:

原因是 % 如果我要上传 20 个文件并且它们都是 jpg 没有问题,但如果其中一个文件名是 20%.jpg 或 50%.jpg 我无法上传它们。

我可以编辑和查看我硬盘上的图像,但我无法上传它们。

例如,对于文件索引 0 中的测试,我有:D:\5%.jpg 所以 txf 和 fn 都包含:D:\5%.jpg

这是我将文件上传到我的 ftp 服务器的方法的顶部:

当我在这个方法中调用方法 createDirectory 时发生异常。

private void StringArrayUploadFiles(object sender, DoWorkEventArgs e)
        {
            try
            {
                foreach (string txf in files)
                {
                    string fn = txf;
                    BackgroundWorker bw = sender as BackgroundWorker;
                    FileInfo fileInfo = new FileInfo(txf);
                    f.TargetFolder = fileInfo.DirectoryName;
                    f.TargetFolder = Path.GetDirectoryName(txf);
                    f.TargetFolder = Path.GetFullPath(txf).Replace(":", "").Replace("\\", "/");
                    string filenameonly = Path.GetFileName(f.TargetFolder);
                    int index = txf.IndexOf(filenameonly);
                    string left = txf.Substring(0, index);
                    f.TargetFolder = Path.GetFullPath(left).Replace(":", "").Replace("\\", "");
                    createDirectory(f.TargetFolder);

变量f是FtpSettings:

public class FtpSettings
    {
        public string Host, Username, Password, TargetFolder, SourceFile;
        public bool Passive;
        public int Port = 21;
    }

文件是字符串[]

当我试图在 ftp 服务器上为文件创建目录时发生异常。 那就是我从下载方法调用的方法createDirectory:

createDirectory(f.TargetFolder);

这是 createDirectory 方法:

public void createDirectory(string newDirectory)
        {
            try
            {
                string FtpHost = f.Host + "/" + newDirectory;
                if (!FtpHost.ToLower().StartsWith("ftp://"))
                    FtpHost = "ftp://" + FtpHost;
                ftpRequest = (FtpWebRequest)WebRequest.Create(FtpHost);
                ftpRequest.Credentials = new NetworkCredential(f.Username, f.Password);
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
                ftpResponse1 = (FtpWebResponse)ftpRequest.GetResponse();
                ftpResponse1.Close();
                ftpRequest = null;
            }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            return;
        }

在变量 newDirectory 中我有:D 没关系,所以我不确定 % 在哪里或为什么 % 会出现问题。

createDirectory方法里面的异常就行了:

ftpResponse1 = (FtpWebResponse)ftpRequest.GetResponse();

System.Net.WebException was caught
  HResult=-2146233079
  Message=The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
  Source=System
  StackTrace:
       at System.Net.FtpWebRequest.CheckError()
       at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
       at System.IO.Stream.Close()
       at System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
       at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse)
       at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
       at System.Net.FtpWebRequest.GetResponse()
       at FTP_ProgressBar.FtpProgress.createDirectory(String newDirectory) in c:\ftp_progressbar\FTP_ProgressBar\FtpProgress.cs:line 270
  InnerException:

第 270 行是:

ftpResponse1 = (FtpWebResponse)ftpRequest.GetResponse();

【问题讨论】:

  • 只有名称中包含 % 的文件才会发生异常,例如 test%.jpg 或 5%.jpg 我在 5%.jpg 上检查过,但其他带有 % 的文件也会出现异常.没有 % 的常规文件,例如 5.jpg 或 test.jpg 工作正常。

标签: c# .net winforms ftp


【解决方案1】:

也许是 ftp URI 中的特殊字符,如 20% 或其他? 尝试使用HttpUtility.UrlDecodeHttpUtility.UrlEncode

【讨论】:

  • 我没有找到如何使用它的好例子,但它有什么作用?从文件名中删除空格或 % ?文件名是 5%.jpg 所以它在我的 ftp 服务器上也应该是相同的名称:5%.jpg 而不是 5.jpg
  • URI 包含您不能在请求中使用的特殊字符(例如空格、退格等)。如果你想发送这个特殊符号,你必须将此字符编码为有效的 URI 字符串。在此之后,您的字符串中的特殊字符已替换为“%code”(例如“空格”替换为“%20”)。您在 URI 符号中使用 '%' 表示编码符号的开头,以及 '%.'是不正确的符号。如果您对 URI '5%.jpg' 进行编码,则会得到文件夹 '5%.jpg',但在请求中将发送 URI 有效字符串,例如 '5%25.jpg'。
猜你喜欢
  • 2013-05-30
  • 1970-01-01
  • 1970-01-01
  • 2021-12-25
  • 2015-06-03
  • 1970-01-01
  • 2011-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多