Hi good people of Danniweb,
I've looked at a lot of examples on line and I cannot figure out what is going wrong.
My program has to transfer a simple delimited text file from the local machine up to a specified directory on a FTP server.
After I have run the routine, I can see the file in the correct directory in the FTP server BUT it is empty i.e. no text.
I tried copying the same file to the same directory via windows explorer and it copied up so I don't think there is an issue with rights or firewalls or lack of content.
Here is the routine I use:
public bool UploadFileToFTP(string FileName)
{
Uri serverUri = null;
FtpWebRequest request = null;
FileInfo fi = null;
FileStream sourceStream = null;
byte[] fileContents = null;
Stream requestStream = null;
FtpWebResponse response = null;
int bytesSent;
try
{
if (_FTPURL.Trim() == "")
{
MessageBox.Show("FTP Address has not been specified. Please check your settings.", "No FTP Address", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}
serverUri = new Uri(_FTPURL);
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
//Not a valid FTP address
MessageBox.Show("The FTP Server URL is not in a valid FTP format.", "Unable to Save Settings", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}
if (_FTPUser.Trim() == "")
{
MessageBox.Show("FTP Username has not been specified. Please check your settings.", "No FTP User", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}
if (!File.Exists(FileName))
{
MessageBox.Show("Unable to locate file " + FileName + ". Has it been moved or deleted?", "Missing File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}
fi = new FileInfo(FileName);
if (fi.Length == 0)
{
MessageBox.Show(FileName + " has a file size of zero.", "Empty File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}
//Set up FTP request Object serverUrl is the place to upload to and Fi.Name is just the name of the file we are uploading.
if (_FTPDirectory.Trim() != "")
{
request = (FtpWebRequest)FtpWebRequest.Create(serverUri + string.Format("/{0}/{1}", _FTPDirectory, fi.Name));
//request = (FtpWebRequest)WebRequest.Create(serverUri + string.Format(@"{0}/{1}", _FTPDirectory, fi.Name));
}
else
{
request = (FtpWebRequest)FtpWebRequest.Create(serverUri + "/" + fi.Name);
//request = (FtpWebRequest)WebRequest.Create(serverUri + @"" + fi.Name);
}
/* Log in to the FTP Server with the User Name and Password Provided */
request.Credentials = new NetworkCredential(_FTPUser.Trim(), _FTPPassword.Trim());
/* When in doubt, use these options */
request.UseBinary = true;
request.UsePassive = true;
request.KeepAlive = true;
//Specify we are uploading...
request.Method = WebRequestMethods.Ftp.UploadFile;
//Used for sending binary file content
fileContents = new byte[2048];
//Open the File for Reading as a stream
sourceStream = new FileStream(FileName, FileMode.Open);
//Open the FTP Request stream
requestStream = request.GetRequestStream();
bytesSent = sourceStream.Read(fileContents, 0, 2048);
while (bytesSent != 0)
{
requestStream.Write(fileContents,0, bytesSent);
bytesSent = sourceStream.Read(fileContents, 0, 2048);
}
sourceStream.Close();
requestStream.Flush();
requestStream.Close();
response = (FtpWebResponse)request.GetResponse();
//Let User Know + Tidy up
MessageBox.Show(String.Format("{0} uploaded , status {1}", FileName, response.StatusDescription), FileName + " Uploaded.", MessageBoxButtons.OK, MessageBoxIcon.Information);
response.Close();
response = null;
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error UpLoading File", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
finally
{
//Tidy Up
if (response != null)
{
response = null;
}
if (requestStream != null)
{
requestStream.Dispose();
requestStream = null;
}
if (fileContents != null)
{
fileContents = null;
}
//if (sourceStream != null)
//{
// sourceStream.Close();
// sourceStream.Dispose();
// sourceStream = null;
//}
if (fi != null)
{
fi = null;
}
if (request != null)
{
request = null;
}
if (serverUri != null)
{
serverUri = null;
}
}
}
The message I get from the FTP server is "226 File Uploaded Sucessfully"