C#将XLS文件上传到FTP

时间:2013-12-13 05:47:48

标签: c#

我使用以下代码将* .xls文件上传到FTP。它适用于上传,但FTP中* .xls文件的内容都搞乱了

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

任何人都知道如何解决这个问题????????

1 个答案:

答案 0 :(得分:1)

而不是:

StreamReader sourceStream = new StreamReader(filePath);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();

尝试使用它:

byte[] fileContents = File.ReadAllBytes(filePath);

它可能是编码\解码问题。