Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
protected void LinkButton1_Click(object sender, EventArgs e)
  {
      string url = @"D://Error.txt";
      // Create an instance of WebClient
      WebClient client = new WebClient();
      // Hookup DownloadFileCompleted Event
      client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);

      // Start the download and copy the file to c:\temp
      client.DownloadFileAsync(new Uri(url), @"E:\Error.txt");
  }
  void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
  {
      Response.Write("File downloaded");
  }



Here @"D://Error.txt"; path name from the server system directive...I want to down load a file from server system..but i am not getting any result from this code...please help me?
Posted
Comments
george4986 28-Feb-14 1:35am    
what is ur requirement ?
are u accessing server through lan or internet?
Siva Hyderabad 28-Feb-14 2:10am    
internet..when client to download the docx file
george4986 28-Feb-14 2:16am    
public static string[] DirectoryListing(string FTPDirectory, bool Detailed, string UserName, string Password)
{
try
{


FtpWebRequest FTP = (FtpWebRequest)FtpWebRequest.Create(FTPDirectory);
FTP.Credentials = new NetworkCredential(UserName, Password);
FTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
if (!Detailed)
FTP.Method = WebRequestMethods.Ftp.ListDirectory;



WebResponse Response = FTP.GetResponse();
StreamReader Reader = new StreamReader(Response.GetResponseStream());

StringBuilder Result = new StringBuilder();
string Line = Reader.ReadLine();
while (Line != null)
{
Result.Append(Line);
Result.Append("\n");
Line = Reader.ReadLine();
}

Result.Remove(Result.ToString().LastIndexOf("\n"), 1);
Reader.Close();
Response.Close();
return Result.ToString().Split('\n');
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

public static long FileSize(string FTPFullFileName, string UserName, string Password)
{
try
{

FtpWebRequest FTP = (FtpWebRequest)FtpWebRequest.Create(FTPFullFileName);
FTP.Method = WebRequestMethods.Ftp.GetFileSize;
FTP.UseBinary = true;
FTP.Credentials = new NetworkCredential(UserName, Password);
FtpWebResponse Response = (FtpWebResponse)FTP.GetResponse();
Stream FtpStream = Response.GetResponseStream();
long FileSize = Response.ContentLength;

FtpStream.Close();
Response.Close();
return FileSize;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

public static void Rename(string FTPFullFileName, string NewFileName, string UserName, string Password)
{
try
{

FtpWebRequest FTP = (FtpWebRequest)FtpWebRequest.Create(FTPFullFileName);
FTP.Method = WebRequestMethods.Ftp.Rename;
FTP.RenameTo = NewFileName;
FTP.UseBinary = true;
FTP.Credentials = new NetworkCredential(UserName, Password);
FtpWebResponse Response = (FtpWebResponse)FTP.GetResponse();
Stream FtpStream = Response.GetResponseStream();
FtpStream.Close();
Response.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

public static void MakeDirectory(string FTPDirectory, string UserName, string Password)
{
try
{
FtpWebRequest FTP = (FtpWebRequest)FtpWebRequest.Create(FTPDirectory);
FTP.Method = WebRequestMethods.Ftp.MakeDirectory;
FTP.UseBinary = true;
FTP.Credentials = new NetworkCredential(UserName, Password);
FtpWebResponse Response = (FtpWebResponse)FTP.GetResponse();
Stream FtpStream = Response.GetResponseStream();

FtpStream.Close();
Response.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
george4986 28-Feb-14 2:15am    
select functions necessary

public static void Upload(string FullFileName, string FTPFullFileName, string UserName, string Password)
{
try
{
FileInfo File = new FileInfo(FullFileName);
FtpWebRequest FTP = (FtpWebRequest)FtpWebRequest.Create(FTPFullFileName);
FTP.Credentials = new NetworkCredential(UserName, Password);
FTP.KeepAlive = false;
FTP.Method = WebRequestMethods.Ftp.UploadFile;
FTP.UseBinary = true;
FTP.ContentLength = File.Length;
FTP.UsePassive = false;


int BuffLength = 2048;
byte[] Buffer = new byte[BuffLength];
int contentLen;


FileStream fs = File.OpenRead();


Stream strm = FTP.GetRequestStream();


contentLen = fs.Read(Buffer, 0, BuffLength);


while (contentLen != 0)
{
strm.Write(Buffer, 0, contentLen);
contentLen = fs.Read(Buffer, 0, BuffLength);
}


strm.Close();
fs.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

public static void Download(string FTPFullFileName, string DestFullFileName, string UserName, string Password)
{
try
{

DestFullFileName = DestFullFileName.Replace("/", "\\");
int indexPart = DestFullFileName.LastIndexOf("\\");

if (indexPart != -1)
{
String DestFolder = DestFullFileName.Substring(0, indexPart + 1);

if (!Directory.Exists(DestFolder))
Directory.CreateDirectory(DestFolder);
}

FtpWebRequest FTP = (FtpWebRequest)FtpWebRequest.Create(FTPFullFileName);
FTP.Method = WebRequestMethods.Ftp.DownloadFile;
FTP.UseBinary = true;

FTP.Proxy = null;
//FTP.KeepAlive = false;
//FTP.UsePassive = false;

FTP.Credentials = new NetworkCredential(UserName, Password);

FtpWebResponse Response = (FtpWebResponse)FTP.GetResponse();
Stream FtpStream = Response.GetResponseStream();
int BufferSize =Convert.ToInt32(FileSize(FTPFullFileName, UserName, Password)) ;
//int BufferSize = 2048;
byte[] Buffer = new byte[BufferSize];

FileStream OutputStream = new FileStream(DestFullFileName, FileMode.Create);
int ReadCount = FtpStream.Read(Buffer, 0, BufferSize);
while (ReadCount > 0)
{
OutputStream.Write(Buffer, 0, ReadCount);
ReadCount = FtpStream.Read(Buffer, 0, BufferSize);
}

FtpStream.Close();
OutputStream.Close();
Response.Close();
}
catch (Exception ex)
{
// throw new Exception(ex.Message);
}

}

Siva Hyderabad 28-Feb-14 2:19am    
thanx bro...i will check now..

1 solution

Where is downloading? You are trying to copy one local file to another. No need to use the WebClient for that, just copy the file. :-)

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900