2012-10-28 140 views
0

使用下面的代碼我試圖將加密文件上傳到FTP服務器。 從ftp服務器下載文件時,它們全部損壞。通過FTP傳輸加密文件,解密時損壞

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url); 
request.Method = WebRequestMethods.Ftp.UploadFile; 
request.Credentials = new NetworkCredential(
    user, 
    pass); 

StreamReader sourceStream = new StreamReader(tempfilepath); 
byte[] fileContents = ASCIIEncoding.ASCII.GetBytes(sourceStream.ReadToEnd()); // uploads corrupted files 
//byte[] fileContents = File.ReadAllBytes(tempfilepath); 
sourceStream.Close(); 
request.ContentLength = fileContents.Length; 

Stream requestStream = request.GetRequestStream(); 
requestStream.Write(fileContents, 0, fileContents.Length); 
requestStream.Close(); 

// Shows confirm message 
FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 
response.Close(); 

任何線索可能是什麼問題?

+0

不要往返ASCII。這會破壞所有字節> 127。 – CodesInChaos

+1

取消註釋'byte [] fileContents = File.ReadAllBytes(tempfilepath);' –

回答

2

它看起來像你正在讀取任意二進制數據到StreamReader
不要這樣做。

StreamReader s read text;將原始二進制數據傳遞到StreamReader將刪除所有無效的代碼點。

相反,你應該純粹與Streambyte[]工作。

0

問題解決了...不幸的是,這是一個非常愚蠢的問題,所以它不會真的幫助任何人。 我實際上傳了未加密的文件,所以當我解密它時,顯然它不會工作。