【问题标题】:FTP Request - Response: Upload - Download. Which Stream do I have to use?FTP 请求 - 响应:上传 - 下载。我必须使用哪个流?
【发布时间】: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之后返回。

【问题讨论】:

    标签: c# ftp stream


    【解决方案1】:

    这里的问题是

    response.GetResponseStream().Length
    

    要么是因为这种类型的 Stream 不支持 Length 属性,要么更可能是因为您在同一个响应对象上调用了两次 GetResponseStream。我猜你只能这样做一次,然后使用这个 Stream 一次。

    你应该使用

    response.ContentLength
    

    而不是获取 StreamWithTransferSpeed 的字节数。

    【讨论】:

    • 谢谢。现在它可以工作,但目前我在StreamWithTransferSpeed.Read(..) 或.ReadByte(..) 添加this.ReportProgress(count);,进程值是负的,因为response.ContentLength = -1。知道为什么吗?
    • 因为您的 FTP 服务器不会告诉您文件大小。在下载之前尝试使用 WebRequestMethods.Ftp.GetFileSize 找出大小;
    • 我认为this.ReportProgress(count); 的位置不正确。 ftpContentLength (WRM.Ftp.GetFileSize;) 是正确的,但 e.ProgressPercentage 超过 100% (214%)。它与上传时的等式相同。我认为这个public override int Read(byte[] buffer, int offset, int count) { this.ReportProgress(count); return this.innerStream.Read(buffer, offset, count); } 不正确。
    • 我添加了一个断点,发现this.totalBytes 增加了ContentLength 以上。 atm 不知道为什么。我明天会检查这个。太好了,我们可以再次创建新的聊天。
    猜你喜欢
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 2016-12-02
    • 1970-01-01
    相关资源
    最近更新 更多