我已經問了一個有關FTP上傳(here)
的問題,我想我的理解是,
但:
如果我使用下面的代碼上傳文件,我可以改變流下載它?
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();
上傳:
源插播= File.OpenRead(文件);
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
- 方法是更經常調用的
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之後。這裏
謝謝。現在它可以工作,但是現在我在'StreamWithTransferSpeed.Read(..)'或'.ReadByte(..)'處添加'this.ReportProgress(count);',progess值是負數,因爲' response.ContentLength' = -1。任何想法爲什麼? – Ismoh
因爲您的FTP服務器不會告訴您文件大小。嘗試使用WebRequestMethods.Ftp.GetFileSize在下載之前找出大小; – Frank
謝謝,我會嘗試。 – Ismoh