Jose Saiz

Jose Saiz

  • 1.5k
  • 260
  • 103.1k

FTP Upload Problem

Dec 19 2016 9:31 AM

  1. FileInfo fileInfo = new FileInfo(filename);  
  2.   
  3.   
  4.                     string sFtpHostSite = "ftp://" + _sFtpHost.Replace("\0""");  
  5.   
  6.                     //- Get the object used to communicate with the server.  
  7.                     FtpWebRequest request = (FtpWebRequest)WebRequest.Create(sFtpHostSite + @"/" + fileInfo.Name);                      
  8.                     request.Method = WebRequestMethods.Ftp.UploadFile;  
  9.                     //- FTP site login credential.  
  10.                     request.Credentials = new NetworkCredential(_sFtpUserId.Trim().Replace("\0"""), _sFtpPwd.Trim().Replace("\0"""));  
  11.                     request.KeepAlive = false;  
  12.                     request.Method = "STOR";  
  13.                     request.UseBinary = true;  
  14.   
  15.                     // Copy the contents of the file to the request stream.  
  16.                    
  17.                     StreamReader sourceStream = new StreamReader(_sLocalPath.Replace("\0","") + fileInfo.Name);  
  18.                     byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());  
  19.                     sourceStream.Close();  
  20.                     request.ContentLength = fileContents.Length;  
  21.   
  22.                     Stream requestStream = request.GetRequestStream();  
  23.                     requestStream.Write(fileContents, 0, fileContents.Length);  
  24.                     requestStream.Close();  
  25.                     //- Wrapping up  
  26.                     FtpWebResponse response = (FtpWebResponse)request.GetResponse();  
  27.   
  28.                     Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);  
  29.                     //- Good Bye  
  30.                     response.Close();  
  31.                 }  
  32.                 catch (Exception oErMsg1)  
  33.                 {  
  34.                    
  35.                     Console.WriteLine(oErMsg1.Message, "Error Occurred while uploading file to FTP server.");  
  36.                    
  37.                 }  
 Does anyone knows why when the file gets uploaded to the FTP-Server it is not the same as the Local-directory?

Explain:

I compress zip some folders on the local computer using 7z library when I open the compressed zip file on my local for test it open and I can unzip it with no problem but once I upload it using my c# FTP-Upload the size of the file is different and when I download it regardless the FTP client used for download the file is corrupted I can't open it.
 
Please see my code for more detail
 
Thanks in advance
AL 
 

Answers (4)