I am trying to upload a .txt file to a ftp server. But i am having some issues. I keep getting this error: The remote server returned an error: (553) File name not allowed.
This always happens on this line:
Stream writer = ftpReq.GetRequestStream();
I cant figure out what the problem is. I have tried uploading the file with filezilla and that works fine. I also have tried to copy the file destination url from the server and harcode it into my application, but i still get the same error. Is there any other ways of uploading a file to a ftp server??
this is my code:
string localFilePath = @"\\fileprint\data\Groups\Operation\fileExports\folder\";
string archiveFilePath = @"\\fileprint\data\Groups\Operation\fileExports\folder\Archive\";
string logFilePath = @"C:\Users\lmy\Desktop\Logs";
string ftpServer = "ftp://my.server.no:21/home/pll332/tmp/";
private string logFileName = "" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
string userName = "pll332";
string password = "password";
public Controller()
{
}
public void UploadFile()
{
try
{
string[] files = Directory.GetFiles(localFilePath);
foreach (string file in files)
{
string fileName = Path.GetFileName(file);
FtpWebRequest ftpReq = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServer + fileName));
ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
ftpReq.UsePassive = true;
ftpReq.UseBinary = true;
ftpReq.KeepAlive = true;
ftpReq.Credentials = new NetworkCredential(userName.Normalize(), password.Normalize());
FileInfo fileInfo = new FileInfo(localFilePath + @"\" + fileName);
FileStream fileStream = fileInfo.OpenRead();
byte[] fileContent = new byte[fileInfo.Length];
fileStream.Read(fileContent, 0, Convert.ToInt32(fileInfo.Length));
Stream writer = ftpReq.GetRequestStream();
writer.Write(fileContent, 0, fileContent.Length);
fileStream.Close();
writer.Close();
FtpWebResponse response = (FtpWebResponse)ftpReq.GetResponse();
AppendLogFile(response, "Uploaded Files: ", fileName);
MoveToArchive(file, archiveFilePath + fileName);
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
Hope you guys can help me. thanks!
EDIT: I have tried this:
string fileName = Path.GetFileName(file);
UriBuilder uri = new UriBuilder();
uri.Scheme = "ftp";
uri.Host = "my.server.no";
uri.Port = 21;
uri.Path = "/home/username/tmp/";
uri.UserName = "username";
uri.Password = "password";
FtpWebRequest ftpReq = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri.ToString() + fileName));
ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
ftpReq.UsePassive = true;
ftpReq.UseBinary = true;
ftpReq.KeepAlive = true;
ftpReq.Credentials = new NetworkCredential(userName.Normalize(), password.Normalize());
This gives me the same error......