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.