为什么上传文件格式20%.jpg我得到例外?

时间:2014-12-16 11:17:36

标签: c# .net winforms ftp

原因是% 如果我上传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方法中的异常heppen:

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();

1 个答案:

答案 0 :(得分:2)

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