Click here to Skip to main content
15,886,033 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to develop a windows application, which is by clicking on button, some files should be downloaded from the server. Download link will be give in code.

So could you please help me to solve this problem?
Posted

try this function

C#
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.Credentials = new NetworkCredential(UserName, Password);

               FtpWebResponse Response = (FtpWebResponse)FTP.GetResponse();
               Stream FtpStream = Response.GetResponseStream();
               int BufferSize =Convert.ToInt32(FileSize(FTPFullFileName, UserName, Password)) ;
                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);
           }

       }



good luck ;-)
 
Share this answer
 
Comments
george4986 18-Sep-14 6:27am    
namespaces used
using System.Windows.Forms;
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Data;
I hope you can do it easily.

C#
WebClient client = new WebClient();
           client.DownloadFile("URL here", "File Name HERE");
 
Share this answer
 
try this,it may help you.

C#
string filePath1 = (sender as LinkButton).CommandArgument;
            string filepath = ("D:\\ex\\" + filePath1);  // ex is your downloading filename
            FileInfo myfile = new FileInfo(filepath);
            if (filePath1 != "")
            {
                Response.ClearContent();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);
                Response.AddHeader("Content-Length", myfile.Length.ToString());
                Response.ContentType = ReturnExtension(myfile.Extension.ToLower());
                Response.TransmitFile(myfile.FullName);
                Response.End();
 
Share this answer
 
Comments
Aboobakkar Siddeq D U 18-Sep-14 3:38am    
Could you please tell me which namespace i have to use to get "Response". Im getting error like
"The name 'Response' does not exist in the current context"
Aboobakkar Siddeq D U 18-Sep-14 5:34am    
Sorry, I'm doing windows application, not a web application.

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