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"

Hi,
Yes, i'm streaming the file up and I can see there is a file size of 153 bytes as I said it is a small simple text file. ( I check the file size before I even try to connect to the FTP server.)

I've had this in the past and i felt pretty stupid but there wasn't enough space to actually create the file which will probably not be the case seeing as the file in question is so small (But still). Another issue i've come accross was permissions. Try uploading the file without code via windows explorer or another 3rd party application and see if that works.

Hi all,

I feel really stupid but it wasn't my code!!! When I looked at the file size on the clients FTP server it was 1 kb I got my client to open the file on their server and it did have data. So I went back into my windows explorer and tried opening the file - it opened as a blank!! I then selected the option of copying the file to my local hard drive and it was populated. so something funny was happening when I was op the file from the FTP server other than copying it down.

This question has already been answered. Start a new discussion instead.