Reputation: 43
I use following code to upload *.xls file into FTP. It works fine for uploading, but the content of *.xls file in FTP all mess up
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(@"ftp://www.xxxxxx.com" + fileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("username", "password");
StreamReader sourceStream = new StreamReader(filePath);
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();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
Any one know how to fix this problem???????
Upvotes: 0
Views: 2247
Reputation: 7445
Insted of this:
StreamReader sourceStream = new StreamReader(filePath);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
Try to use this:
byte[] fileContents = File.ReadAllBytes(filePath);
It might be encoding\decoding problem.
Upvotes: 2