- FileInfo fileInfo = new FileInfo(filename);
- string sFtpHostSite = "ftp://" + _sFtpHost.Replace("\0", "");
- //- Get the object used to communicate with the server.
- FtpWebRequest request = (FtpWebRequest)WebRequest.Create(sFtpHostSite + @"/" + fileInfo.Name);
- request.Method = WebRequestMethods.Ftp.UploadFile;
- //- FTP site login credential.
- request.Credentials = new NetworkCredential(_sFtpUserId.Trim().Replace("\0", ""), _sFtpPwd.Trim().Replace("\0", ""));
- request.KeepAlive = false;
- request.Method = "STOR";
- request.UseBinary = true;
- // Copy the contents of the file to the request stream.
- StreamReader sourceStream = new StreamReader(_sLocalPath.Replace("\0","") + fileInfo.Name);
- byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
- sourceStream.Close();
- request.ContentLength = fileContents.Length;
- Stream requestStream = request.GetRequestStream();
- requestStream.Write(fileContents, 0, fileContents.Length);
- requestStream.Close();
- //- Wrapping up
- FtpWebResponse response = (FtpWebResponse)request.GetResponse();
- Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
- //- Good Bye
- response.Close();
- }
- catch (Exception oErMsg1)
- {
- Console.WriteLine(oErMsg1.Message, "Error Occurred while uploading file to FTP server.");
- }
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
Prakash KumarPosted Dec 19, 2016, 10:53 PM
Jose SaizPosted Dec 21, 2016, 8:12 AM
Thank you very much Prakash Kumar that solved my problem using BinaryReader instead of the StreamReader.
Jose SaizPosted Dec 19, 2016, 11:35 AM
Khaja MoizuddinPosted Dec 19, 2016, 11:05 AM