2012-04-26 222 views
12

我创建一个简单的拖放文件和上传,自动对FTP的Windows应用程序将文件上传到FTP目的地

enter image description here

被破坏一次,我使用MSDN code到将文件上传到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

enter image description here

问题是当我看到在浏览器上的文件,或者干脆下载并尝试在桌面上看到我得到:

enter image description here

我已经使用request.UseBinary = false;request.UsePassive = false;但它没有缝隙做任何好事。

我已经找到了是,原来的文件有122KB lenght并在FTP(和下载后),它拥有219KB ...

我在做什么错?

顺便说一句,该uploadFileToFTP()方法是BackgroundWorker内运行,但我真的不使任何差异的事情...

+0

二进制数据和UTF-8不拌匀。 – dtb 2012-04-26 09:28:40

+0

@dtb使用'Encoding.ASCII.GetBytes(sourceStream.ReadToEnd());'得到相同的行为... – balexandre 2012-04-26 09:30:32

+0

感叹。 **二进制**数据和**字符**数据是两个不同的东西。是的,您可以将字符编码为字节,但不能盲目解码不编码字符的字节。 – dtb 2012-04-26 09:32:03

回答

31

你不应该使用一个StreamReader,但只有一个流中读取二进制文件。

Streamreader仅用于读取文本文件。

尝试这样的:

private static void up(string sourceFile, string targetFile) 
{    
    try 
    { 
     string ftpServerIP = ConfigurationManager.AppSettings["ftpIP"]; 
     string ftpUserID = ConfigurationManager.AppSettings["ftpUser"]; 
     string ftpPassword = ConfigurationManager.AppSettings["ftpPass"]; 
     ////string ftpURI = ""; 
     string filename = "ftp://" + ftpServerIP + "//" + targetFile; 
     FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create(filename); 
     ftpReq.UseBinary = true; 
     ftpReq.Method = WebRequestMethods.Ftp.UploadFile; 
     ftpReq.Credentials = new NetworkCredential(ftpUserID, ftpPassword); 

     byte[] b = File.ReadAllBytes(sourceFile); 

     ftpReq.ContentLength = b.Length; 
     using (Stream s = ftpReq.GetRequestStream()) 
     { 
      s.Write(b, 0, b.Length); 
     } 

     FtpWebResponse ftpResp = (FtpWebResponse)ftpReq.GetResponse(); 

     if (ftpResp != null) 
     { 
      MessageBox.Show(ftpResp.StatusDescription); 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.ToString()); 
    } 
} 
+0

那一个工作。谢谢! – 2013-07-20 11:29:43

+1

@ user2212907:如果它帮助你,upvote答案,以便它可以帮助别人;) – LaGrandMere 2013-07-22 12:19:39

+0

对不起!我忘了上传:P 完成! – 2013-07-22 12:42:52

3

的问题通过你的代码的二进制数据字符解码引起数据并返回到二进制数据。不要这样做。


使用WebClient ClassUploadFile Method

using (WebClient client = new WebClient()) 
{ 
    client.Credentials = new NetworkCredential(FTP_USR, FTP_PWD); 
    client.UploadFile(FTP_PATH + filenameToUpload, filenameToUpload); 
} 
+1

该类是不好的。引发像地狱般的异常。未能执行上传。 – 2013-07-19 10:45:27

+0

你救了我的人生,为什么人们使用那个令人讨厌的代码,这是最好的+1 – 2015-10-25 07:01:10