1

I am trying to read a CSV file to upload it to a database Table using the following code:

FtpWebRequest reqFTP;
try
{
    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileName));
    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
    reqFTP.UseBinary = true;
    reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
    int count=0;
    StringBuilder sb = new StringBuilder();
    //GET THE FTP RESPONSE
    using (System.Net.WebResponse tmpRes = reqFTP.GetResponse()) 
    //^^^^^^^^^^^^^^ Error is on this line ^^^^^^^^^^^^^^
    {
        ...
    }
}

When the file name contains a space (which sometimes shows as %20 while debugging) i get the following error:

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

If the filename does not contain a space or %20 it gets read fine.

The task involves to read the file parse contents save data in database and then move file in another folder.

Expert Novice
  • 1,943
  • 4
  • 22
  • 47
  • [Enable logging](http://stackoverflow.com/q/9664650/850848) and show us the log. As well as the actual value of `ftpPath + fileName`. – Martin Prikryl Jul 27 '15 at 10:43

1 Answers1

0

Whenever you are gathering or setting your filenames, try this:

if (filename.Contains(" "))
{
    filename= filename.Replace(" ", Uri.HexEscape(' '));
}

If you plan to move the file afterwards, make sure you go thru and do the opposite before the move. In your case:

if (filename.Contains("%20"))
    {
        filename= filename.Replace("%20", ' ');
    }

This idea can be expand for all unacceptable characters like "#", or "'", or "\" and so forth.

John Boling
  • 464
  • 6
  • 14