Reputation: 1310
I tried to download files, but all files with special character cannot be recognized. Other files can be downloaded, while file named asdf#[email protected]
cannot be downloaded.
Error:
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
In local, the file with correct name is created, but it is empty. The same thing happens on JPG files with #
inside of the file names. how can I let them be recognized?
//Download the file from remote path on FTP to local path
private static void Download(string remotePath, string localPath)
{
FtpWebRequest reqFTP;
try
{
reqFTP = GetWebRequest(WebRequestMethods.Ftp.DownloadFile, remotePath);
FileStream outputStream = new FileStream(localPath, FileMode.Create);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
Console.WriteLine("File Download: ", remotePath + " is downloaded completely");
logWriter.WriteLog("File Download: ", remotePath + " is downloaded completely, status " + response.StatusDescription);
}
catch (Exception ex)
{
logWriter.WriteLog("File Download: ", "Cannot download file from " + remotePath + " to " + localPath + "\n" + " Erro Message: " + ex.Message);
}
}//End Download
//Web request for FTP
static public FtpWebRequest GetWebRequest(string method, string uri)
{
Uri serverUri = new Uri(uri);
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return null;
}
try
{
var reqFTP = (FtpWebRequest)FtpWebRequest.Create(serverUri);
reqFTP.Method = method;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(userId, password);
reqFTP.Proxy = null;
reqFTP.KeepAlive = false;
reqFTP.UsePassive = false;
return reqFTP;
}
catch(Exception ex)
{
logWriter.WriteLog("Get Web Request: ","Cannot connect to " + uri + "\n" + "Error: " + ex.Message);
return null;
}
}
Upvotes: 4
Views: 7066
Reputation: 172270
This might be by design: According to the URI standard, #
is not a valid character in a URI. Thus, ftp://someServer/somePath/intro_to_c#.pdf
is not a valid URI.
What you could do is to properly escape the file name when creating the URI:
string baseUri = "ftp://someServer/somePath/";
string file = "intro_to_c#.pdf";
string myUri = baseUri + HttpUtility.UrlEncode(file);
// yields ftp://someServer/somePath/intro_to_c%23.pdf
Alternatively, you could use the UriBuilder class, which handles escaping properly:
Uri myUri = new UriBuilder("ftp", "someServer", 21, "somePath/intro_to_c#.pdf");
// yields ftp://someServer:21/somePath/intro_to_c%23.pdf
Upvotes: 6
Reputation: 1310
I add some code and fixed it. Use hexEscape to escape "#" but it is not decent. Any one have idea to escape special characters in URI?
// Get the request using a specific URI
static public FtpWebRequest GetWebRequest(string method, string uri)
{
Uri serverUri = new Uri(uri);
**if (serverUri.ToString().Contains("#"))
{
serverUri = new Uri(serverUri.ToString().Replace("#", Uri.HexEscape('#')));
}**
Console.WriteLine(serverUri.ToString());
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return null;
}
try
{
var reqFTP = (FtpWebRequest)FtpWebRequest.Create(serverUri);
reqFTP.Method = method;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(userId, password);
reqFTP.Proxy = null;
reqFTP.KeepAlive = false;
reqFTP.UsePassive = false;
return reqFTP;
}
catch (Exception ex)
{
logWriter.WriteLog("Get Web Request: ", "Cannot connect to " + uri + "\n" + "Error: " + ex.Message);
return null;
}
}
Upvotes: 1