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 :

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);

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


                lblStatus.Text = "Done";
            catch (Exception ex)
                lblStatus.Text = "Error";
                this.Cursor = Cursors.Default;


what am i doing wrong? help please.

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;
            FtpWebResponse uploadResponse = (FtpWebResponse)ftpClient.GetResponse();
        string value = uploadResponse.StatusDescription;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.