2014-12-26 16 views
0

我已經問了一個有關FTP上傳(here
的問題,我想我的理解是,


如果我使用下面的代碼上傳文件,我可以改變流下載它?
FTP請求 - 響應:上傳 - 下載。我必須使用哪個流?

string sourcePath = sourceToDestinationPair.Key; 
string destinationPath = sourceToDestinationPair.Value; 
string fileName = new FileInfo(sourcePath).Name; 

Console.WriteLine("Create WebRequest: (Upload) " + destinationPath + "//" + fileName); 

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(destinationPath + "//" + fileName); 
request.Method = WebRequestMethods.Ftp.UploadFile; 
request.Credentials = new NetworkCredential(Manager._user, Manager._password); 

var response = (FtpWebResponse)request.GetResponse(); 
using (Stream source = File.OpenRead(sourcePath)) 
    { 
      using (var destination = new StreamWithTransferSpeed(request.GetRequestStream(), bGWorkerUpload, source.Length, fileName))//request.ContentLength)) 
      { 
       source.CopyTo(destination); 
      } 
    } 
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription); 
response.Close(); 


上傳:
源插播= File.OpenRead(文件);
destination-Stream = WebRequest.GetRequestStream;

Dwonload ;:
源插播=網頁..... ????
destination-Stream = File.OpenWrite(file);


我以爲源流在下載爲WebResponse.GetResponseStream();,而是拋出異常:
"System.NotSupportedException": This Stream do not support any search pattern
這是我嘗試:

string sourcePath = sourceToDestinationPair.Key; 
string destinationPath = sourceToDestinationPair.Value; 
string fileName = new FileInfo(sourcePath).Name; 

Console.WriteLine("Create WebRequest: (Download) " + destinationPath + "//" + fileName); 
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(destinationPath + "//" + fileName); 
request.Method = WebRequestMethods.Ftp.DownloadFile; 
request.Credentials = new NetworkCredential(Manager._user, Manager._password); 

var response = (FtpWebResponse)request.GetResponse(); 
using (var source = new StreamWithTransferSpeed(response.GetResponseStream(), bGWorkerDownload, response.GetResponseStream().Length, fileName)) 
{ 
    using (var destination = File.OpenWrite(sourcePath))//request.GetResponse().GetResponseStream(), bGWorkerDownload, request.GetResponse().ContentLength, fileName)) 
    { 
      source.CopyTo(destination); 
    } 
} 
Console.WriteLine("Download File Complete, status {0}", response.StatusDescription); 
response.Close(); 


StreamWithTransferSpeed是一個覆蓋流類,你可以在上面的url中看到。
我不知道我在這裏做什麼...

感謝您的幫助。


編輯:
我覺得 this.ReportProgress - 方法是更經常調用的 this.innerStream.Read - 方法,但我不知道爲什麼,我不知道如何去改變它:

public override int Read(byte[] buffer, int offset, int count) 
{ 
    this.ReportProgress(count); 
    return this.innerStream.Read(buffer, offset, count); 
} 


這是Write - 方法和它工作得很好:

public override void Write(byte[] buffer, int offset, int count) 
{ 
    this.innerStream.Write(buffer, offset, count); 
    this.ReportProgress(count); 
} 


我不知道爲什麼,但我估計this.ReportProgess - 方法也必須低於this.innerStream.Read - 方法作爲Write - 方法,我不知道如何來緩存Read - 方法和返回它在ReportProgress之後。這裏

回答

1

問題是

response.GetResponseStream().Length 

要麼是因爲這種類型的流的不支持Length屬性,或者更可能的是,你調用GetResponseStream兩次相同的響應對象。我想你只能做一次,然後一次使用這個Stream。

您應該使用

response.ContentLength 

,而不是得到的字節數爲您StreamWithTransferSpeed。

+0

謝謝。現在它可以工作,但是現在我在'StreamWithTransferSpeed.Read(..)'或'.ReadByte(..)'處添加'this.ReportProgress(count);',progess值是負數,因爲' response.ContentLength' = -1。任何想法爲什麼? – Ismoh

+1

因爲您的FTP服務器不會告訴您文件大小。嘗試使用WebRequestMethods.Ftp.GetFileSize在下載之前找出大小; – Frank

+0

謝謝,我會嘗試。 – Ismoh

相關問題