I'm working on a project, that collects some informations to a .CSV file and then send the file to a ftp server.
The program sends the file successfully sometimes, but other times it just stops and throws the error:
An unhandled exception of type 'System.Net.WebException' occurred in System.dll
Additional information: The remote server returned an error: 227 Entering Passive Mode (192,168,10,170,216,244)
Can anyone spot anything, that I have done wrong?
Thanks in advance.
Here is my code
private void sendFile_Click(object sender, EventArgs e)
{
Upload("ftp://100.64.44.12", "UsernameHere", "PasswordHere", @"C:\Users\Kasper\Documents\testFolder\data.csv");
}
public void Upload(string FTPAddress, string username, string password, string filePath)
{
FileStream stream = File.OpenRead(filePath);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Close();
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress + "/" +
Path.GetFileName(filePath));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = false;
request.KeepAlive = true;
StreamReader sourceStream = new StreamReader("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();
MessageBox.Show("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}