public class HttpRemoteDownload : RemoteDownload
{
public HttpRemoteDownload(string urlString, string descFilePath)
: base(urlString, descFilePath)
{
}
public override bool DownloadFile()
{
string fileName = System.IO.Path.GetFileName(this.UrlString);
string descFilePathAndName =
System.IO.Path.Combine(this.DescFilePath, fileName);
try
{
WebRequest myre = WebRequest.Create(this.UrlString);
}
catch
{
return false;
}
try
{
byte[] fileData;
using (WebClient client = new WebClient())
{
fileData = client.DownloadData(this.UrlString);
}
using (FileStream fs =
new FileStream(descFilePathAndName, FileMode.OpenOrCreate))
{
fs.Write(fileData, 0, fileData.Length);
}
return true;
}
catch (Exception ex)
{
throw new Exception("download field", ex.InnerException);
}
}
}
public class FtpRemoteDownload : RemoteDownload
{
public FtpRemoteDownload(string urlString, string descFilePath)
: base(urlString, descFilePath)
{
}
public override bool DownloadFile()
{
FtpWebRequest reqFTP;
string fileName = System.IO.Path.GetFileName(this.UrlString);
string descFilePathAndName =
System.IO.Path.Combine(this.DescFilePath, fileName);
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(this.UrlString);
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
using (FileStream outputStream = new FileStream(descFilePathAndName, FileMode.OpenOrCreate))
using (FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse())
using (Stream ftpStream = response.GetResponseStream())
{
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
}
return true;
}
catch (Exception ex)
{
throw new Exception("upload failed", ex.InnerException);
}
}
}