我已经问了一个关于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();
上传:
source-Stream = File.OpenRead(file);
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
-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
-Method 并在 ReportProgress 之后返回它。
这里的问题是
response.GetResponseStream().Length
要么是因为这种类型的 Stream 不支持 Length
属性,要么更有可能是在同一响应对象上调用GetResponseStream
两次。我想您只能执行此操作一次,然后使用此流一次。
你应该使用
response.ContentLength
而是获取StreamWithTransferSpeed的字节数。