我已经问了一个关于ftp-upload(here)的问题
我以为我明白了,
的 BUT :
如果我使用以下代码上传文件,我可以更改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();
上传
source-Stream = File.OpenRead(file);
destination-Stream = WebRequest.GetRequestStream;
的 Dwonload;
source-Stream = Web ..... ????
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
是一个被覆盖的Stream-Class,您可以在上面的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之后返回它。
答案 0 :(得分:1)
这里的问题是
response.GetResponseStream().Length
要么是因为这种类型的流不支持Length
属性,要么更有可能在同一个响应对象上调用GetResponseStream
两次。我想你只能这样做一次,然后一次使用这个Stream。
你应该使用
response.ContentLength
而不是获取StreamWithTransferSpeed的字节数。