Click here to Skip to main content
15,946,320 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HI ,

There are TWO Excel Files in my Program which I am trying to upload to a FTP server and then later Download it  and its getting uploaded and downloaded with no error but when i try to open the file i get error message its corrupted or not in correct format i.e

"file format or extension not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."

the code i am using for upload is as follows :


What I have tried:

C#
private void FtpUploadFile(string filename)
        {
            string user_name = "username";
            string password = "password";
            
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.ftpsite.com/excel.xlsx");
            request.Method = WebRequestMethods.Ftp.UploadFile;

            // Get network credentials.
            request.Credentials = new NetworkCredential(user_name, password);

            // Read the file's contents into a byte array.
            byte[] bytes = System.IO.File.ReadAllBytes(filename);

            // Write the bytes into the request stream.
            request.ContentLength = bytes.Length;
            using (Stream request_stream = request.GetRequestStream())
            {
                request_stream.Write(bytes, 0, bytes.Length);
                request_stream.Close();
            }
        }

        private void btnupload_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                lblStatus.Text = "Working...";
                Application.DoEvents();

                FtpUploadFile(txtFile.Text);

                lblStatus.Text = "Done";
            }
            catch (Exception ex)
            {
                lblStatus.Text = "Error";
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }

            
        }



Help please.
Posted
Updated 28-Feb-17 20:33pm
Comments
Garth J Lancaster 1-Mar-17 0:54am    
I'm not sure how to (or if you can do it with) FtpWebRequest, but, I'd be looking at if you're transferring in Text or Binary mode to start with (it should be Binary)
Nazneen s 1-Mar-17 1:02am    
did not quite get you there @garth
Garth J Lancaster 1-Mar-17 1:17am    
see this https://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.usebinary(v=vs.110).aspx
Nazneen s 1-Mar-17 1:54am    
still not working garth i tried

request.Method = WebRequestMethods.Ftp.UploadFile;
request.UsePassive = true;
request.KeepAlive = false;(and true both)
request.UseBinary = true;
Garth J Lancaster 1-Mar-17 2:03am    
I take it you've checked the obvious,

a) file size in bytes before ftp = file size in bytes after
b) maybe generate a hash of the file on each 'side' and compare

1 solution

i myself came up with the solution just declare all the variables at start ad paste this :

FtpWebRequest ftpClient = (FtpWebRequest)FtpWebRequest.Create(ftpurl + "/" + filename);
            ftpClient.Credentials = new System.Net.NetworkCredential(ftpusername, ftppassword);
            ftpClient.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
            ftpClient.UseBinary = true;
            ftpClient.KeepAlive = true;
            System.IO.FileInfo fi = new System.IO.FileInfo(fileurl);
            ftpClient.ContentLength = fi.Length;
            byte[] buffer = new byte[4097];
            int bytes = 0;
            int total_bytes = (int)fi.Length;
            System.IO.FileStream fs = fi.OpenRead();
            System.IO.Stream rs = ftpClient.GetRequestStream();
            while (total_bytes > 0)
            {
                bytes = fs.Read(buffer, 0, buffer.Length);
                rs.Write(buffer, 0, bytes);
                total_bytes = total_bytes - bytes;
            }
            //fs.Flush();
            fs.Close();
            rs.Close();
            FtpWebResponse uploadResponse = (FtpWebResponse)ftpClient.GetResponse();
        string value = uploadResponse.StatusDescription;
            uploadResponse.Close();


Thanks for the help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900