I am trying to use this code to upload a file to an FTP, the problem I have is that when the syntax hits the serverURI.Scheme != Uri.UriSchemeFtp
it returns false. Does that mean I have my URI address set-up incorrectly? I know it is a valid address, I have used ftptest.net to verify the site is up and running. What is incorrect in my syntax?
private void button1_Click(object sender, EventArgs e)
{
Uri serverUri = new Uri("ftps://afjafaj.org");
string userName = "Ricard";
string password = "";
string filename = "C:\\Book1.xlsx";
ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertificatePolicy;
UploadFile(serverUri, userName, password, filename);
}
public bool AcceptAllCertificatePolicy(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
public bool UploadFile(Uri serverUri, string userName, string password, string fileName)
{
if (serverUri.Scheme != Uri.UriSchemeFtp)
return false;
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.EnableSsl = true;
request.Credentials = new NetworkCredential(userName, password);
request.Method = WebRequestMethods.Ftp.UploadFile;
StreamReader sourceStream = new StreamReader(fileName);
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();
Console.WriteLine("Response status: {0}", response.StatusDescription);
}
catch (Exception exc)
{
throw exc;
}
return true;
}