0

I already asked a question about ftp-upload (here)
And I thought I understood it,
BUT:

If I use the following code to upload a file, can I change the Streams to download it?

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();


Upload:
source-Stream = File.OpenRead(file);
destination-Stream = WebRequest.GetRequestStream;

Dwonload;:
source-Stream = Web..... ????
destination-Stream = File.OpenWrite(file);


I thought the source-Stream at a Download is WebResponse.GetResponseStream();, but a exception is thrown:
"System.NotSupportedException": This Stream do not support any search pattern
That's what I try:

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 is an overwritten Stream-Class, as you can see at the url above.
I dont know what I am doing here...

Thanks for help.


EDIT:
I think the this.ReportProgress-Method is more often invokes as the this.innerStream.Read-Method, but I dont know why and I dont know how to change it:

public override int Read(byte[] buffer, int offset, int count)
{
    this.ReportProgress(count);
    return this.innerStream.Read(buffer, offset, count);
}


This is the Write-Method and it works well:

public override void Write(byte[] buffer, int offset, int count)
{
    this.innerStream.Write(buffer, offset, count);
    this.ReportProgress(count);
}


I dont know why, but I reckon that the this.ReportProgess-Method also have to be below the this.innerStream.Read-Method as in the Write-Method, BUT I dont know how to cache the Read-Method and return it after the ReportProgress.

Community
  • 1
  • 1
Ismoh
  • 1,074
  • 2
  • 12
  • 35

1 Answers1

1

The problem here is

response.GetResponseStream().Length

either because this type of Stream doesn't support the Length property or, more likely, you're calling GetResponseStream twice on the same response object. I guess you can do this only once and then use this Stream a single time.

You should use

response.ContentLength

instead to get the number of bytes for your StreamWithTransferSpeed.

Frank
  • 4,461
  • 13
  • 28
  • Thanks. For now it works, but at the moment I add `this.ReportProgress(count);` at the `StreamWithTransferSpeed.Read(..)` or `.ReadByte(..)` the progess-value is negativ, because the `response.ContentLength` = -1. Any Idea why? – Ismoh Dec 26 '14 at 20:30
  • 1
    Because your FTP server doesn't tell you the file size. Try to find out the size before downloading using WebRequestMethods.Ftp.GetFileSize; – Frank Dec 26 '14 at 20:34
  • I think the `this.ReportProgress(count);` is not on the correct position. `ftpContentLength (WRM.Ftp.GetFileSize;)` is correct, but the `e.ProgressPercentage` runs over 100% (214%). It is the same equation as the equation at the upload. I think this `public override int Read(byte[] buffer, int offset, int count) { this.ReportProgress(count); return this.innerStream.Read(buffer, offset, count); }` is not correct. – Ismoh Dec 26 '14 at 21:00
  • I added a breakpoint and noticed that `this.totalBytes` increase above the `ContentLength`. Atm I don't know why. I will check this tomorrow. It would be nice, when we can create a new chat again. – Ismoh Dec 26 '14 at 21:41