2014-12-26 17 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

相关问题