Reputation: 347
i want to upload files to ftp but i get an error The given path's format is not supported. i have mention my code below kindly help me. the path giving error , what should i need to keep path like ?
the following is my path this.path = @"ftp://ip address/Requests/";
public bool UploadDocsToFTP(string SourceFilePath, string FileName)
{
string ServerUri = "";
string FTPUserName = "";
string FTPPassword = "";
string ftpURI = "";
try
{
try
{
ServerUri = ConfigurationManager.AppSettings["LinuxFileServerUri"];
FTPUserName = ConfigurationManager.AppSettings["LinuxFtpUserName"];
FTPPassword = ConfigurationManager.AppSettings["LinuxFtpPassword"];
string[] splitfilename = SourceFilePath.Split('\\');
//String businesRegId = splitfilename[2];
//String userRegId = splitfilename[3];
//String folderType = splitfilename[3];
//ftpURI = "ftp://" + ServerUri + "//" + businesRegId + "//" + userRegId;
//if (!string.IsNullOrEmpty(folderType))
// ftpURI += "//" + folderType;
//ServerUri = "ftp://" + ServerUri + "//" + businesRegId + "//" + userRegId + "//";
//if (!string.IsNullOrEmpty(folderType))
// ServerUri += folderType + "//";
// SetMethodRequiresCWD();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ip address/Requests" + FileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(FTPUserName, FTPPassword);
FileStream fs = File.OpenRead(SourceFilePath + FileName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpstream = request.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
}
catch (WebException e)
{
String status = ((FtpWebResponse)e.Response).StatusDescription;
if (UploadDocsToFTP_DirNotExists(SourceFilePath, FileName))
{
return true;
}
return false;
}
}
catch (Exception ex)
{
ex.ToString();
return false;
}
return true;
}
Upvotes: 0
Views: 81
Reputation: 5314
A URL (which is what your path is) cannot contain spaces or other certain characters, so you have to encode it.
You can use System.Net.WebUtility for this.
// instead of this:
// FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ip address/Requests" + FileName);
// do this:
string path = System.Net.WebUtility.UrlEncode("ftp://ip address/Requests" + FileName);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(path);
Upvotes: 1