我创建一个简单的拖放文件和上传,自动对FTP的Windows应用程序
和我使用MSDN代码上传文件到FTP。
该代码是非常直截了当:
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(String.Format("{0}{1}", FTP_PATH, filenameToUpload));
request.Method = WebRequestMethods.Ftp.UploadFile;
// Options
request.UseBinary = true;
request.UsePassive = false;
// FTP Credentials
request.Credentials = new NetworkCredential(FTP_USR, FTP_PWD);
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(fileToUpload.FullName);
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();
writeOutput("Upload File Complete!");
writeOutput("Status: " + response.StatusDescription);
response.Close();
并且它得到上传到FTP
问题是 ,当我看到在浏览器上的文件,或者只需下载并尝试看看它放在桌面上,我得到:
我已经使用request.UseBinary = false;
和request.UsePassive = false;
但它并没有缝做任何形式的任何好。
什么我发现是,原来的文件有122KB lenght并在FTP(和下载后),它具有219KB ...
我究竟做错了什么?
顺便说一句,在uploadFileToFTP()
方法在内部运行BackgroundWorker
,但我真的不认为有什么差别的事情...