Guy's I'm trying to use an example below to FTP a file to my web server but it doesn't seem to work, is there anything obvious that I'm missing on the below? (obviously I've coded out my credentials on the below example):
try
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.server.co.uk/");
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("username","password");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(@"c:\test\testfile.txt");
byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
MessageBox.Show("File Uploaded");
}
catch
{
MessageBox.Show("Something went wrong!");
}
Akhil KharePosted Jul 24, 2014, 6:00 AM
instead of using
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.server.co.uk/");
try with
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.server.co.uk/FileName");
e.g.
string ftpUrl ="ftp://ftp.server.co.uk/";
string fileName =@"c:\test\testfile.txt";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl + "/" + fileName);
Hope it will resolve your problem.
Thanks
mike DelvottiPosted Jul 24, 2014, 7:34 AM