简体   繁体   中英

Why when uploading file format 20%.jpg i'm getting exception?

The reason is the % If i'm uploading 20 files and they are all jpg no problems but if one of the files names is for example 20%.jpg or 50%.jpg i can't upload them.

I can edit and see the images on my hard disk but i can't upload them.

Now for example for the test in files index 0 i have: D:\\5%.jpg So txf and fn both contain: D:\\5%.jpg

This is the top part of the method i upload files to my ftp server:

The exception happen when i'm calling the method createDirectory in this method.

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

The variable f is FtpSettings:

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

files is string[]

The exception happen when i'm trying to create the directory for the file on the ftp server. That's in the method createDirectory i'm calling from the download method:

createDirectory(f.TargetFolder);

This is the createDirectory method:

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;
        }

In the variable newDirectory i have: D That's fine so i'm not sure where the % or why the % make the problem.

The exception heppen inside the createDirectory method on the line:

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:

Line 270 is:

ftpResponse1 = (FtpWebResponse)ftpRequest.GetResponse();

maybe that in ftp URI special characters like 20% or other? try to use HttpUtility.UrlDecode and HttpUtility.UrlEncode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM