Nazneen Sayed

Nazneen Sayed

  • NA
  • 32
  • 4.4k

Excel file corrupts while uploading and downloading from FTP

Mar 1 2017 12:59 AM
 
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);
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;
}
}

Answers (2)