【发布时间】:2014-12-26 00:27:11
【问题描述】:
我已经问过一个关于 ftp-upload 的问题 (here)
我以为我明白了,
但是:
如果我使用以下代码上传文件,我可以更改 Streams 以下载它吗?
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(file);
目的地流 = WebRequest.GetRequestStream;
Dwonload;:
源流 = 网络..... ????
目标流 = 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 是一个被覆盖的 Stream-Class,正如您在上面的 url 中看到的那样。
我不知道我在这里做什么...
感谢您的帮助。
编辑:
我认为
this.ReportProgress-Method 更常作为this.innerStream.Read-Method 调用,但我不知道为什么,也不知道如何更改它: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-Method 也必须低于this.innerStream.Read-Method,就像在Write-Method 中一样,但是我不知道如何缓存Read-方法并在ReportProgress之后返回。
【问题讨论】: