2012-04-26 20 views
12

Tôi đang tạo đơn giản kéo file và upload-tự động-to-ftp cửa sổ ứng dụngfile tải lên FTP đang bị hỏng một lần trong điểm đến

enter image description here

và tôi đang sử dụng để MSDN code tải tệp lên FTP.

Mã này là khá thẳng về phía trước:

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

có được tải lên FTP

enter image description here

Vấn đề là khi tôi nhìn thấy các tập tin trên trình duyệt, hoặc chỉ cần tải xuống và thử xem trên máy tính để bàn, tôi nhận được:

enter image description here

Tôi đã sử dụng request.UseBinary = false;request.UsePassive = false; nhưng không có đường may để làm bất kỳ loại gì tốt.

Những gì tôi đã phát hiện ra là, các tập tin ban đầu có 122Kb chiều dài và trong FTP (và sau khi tải về), nó có 219Kb ...

Tôi đang làm gì sai?

Bằng cách này, phương pháp uploadFileToFTP() đang chạy bên trong một BackgroundWorker, nhưng tôi không thực sự điều đó làm cho bất kỳ sự khác biệt ...

+0

Dữ liệu nhị phân và UTF-8 không trộn đều. – dtb

+0

@dtb sử dụng 'Encoding.ASCII.GetBytes (sourceStream.ReadToEnd());' có cùng hành vi ... – balexandre

+0

Sigh. ** Dữ liệu nhị phân ** và dữ liệu ** ký tự ** là hai thứ khác nhau. Có, bạn có thể mã hóa các ký tự thành byte, nhưng bạn không thể giải mã một cách mù quáng các byte không mã hóa các ký tự. – dtb

Trả lời

31

Bạn không nên sử dụng một StreamReader nhưng chỉ một Stream để đọc tập tin nhị phân.

Trình tạo luồng được thiết kế để chỉ đọc tệp văn bản.

Hãy thử với điều này:

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

Điều đó đã hiệu quả. Cảm ơn! –

+1

@ user2212907: nếu nó giúp bạn, hãy đưa ra câu trả lời để giúp người khác;) – LaGrandMere

+0

Xin lỗi! Tôi quên upvote: P Xong! –

3

Những vấn đề được gây ra bởi mã của bạn giải mã các dữ liệu nhị phân để nhân vật dữ liệu và trở lại dữ liệu nhị phân. Đừng làm thế.


Sử dụng UploadFile Method của WebClient Class:

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

lớp học bị trùng lặp gần đó. Ném ngoại lệ như địa ngục. Không thể thực hiện tải lên. –

+0

Bạn đã lưu cuộc sống của tôi bro, tại sao mọi người sử dụng mã gây phiền nhiễu đó, đây là +1 tốt nhất –

Các vấn đề liên quan