Download any type of file from FTP server in ASP.NET (C#).

Aman Sharma
0
We can download all type of file like  CSV, Docx, PNG, JPG, PDF etc from FTP server in asp.net using c#. I have share the code in this article.. We can download any type of file from server using this code. Just change the Extension of the file or mention multiple files extension.

Code to Download All type of Files(CSV, Docx, PNG, JPG, PDFfrom FTP Server:


protected void Page_Load(object sender, EventArgs e)
    {
        string FtpServer="ftp://--.--.--.- /";
        string username="username";
        string password="password";
        string localpath=@"D:\\FolderName\";
        DownloadFile(FtpServer,username,password,localpath);               
           
    }

 public void DownloadFile(string FtpServer,string username,string password,string localpath)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FtpServer);
        request.Credentials = new NetworkCredential(username,password );
        request.Method = WebRequestMethods.Ftp.ListDirectory;
        StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());
        string fileName3 = streamReader.ReadLine();
        List<string> directories = new List<string>();

        while (fileName3 != null && fileName3 != "testing12")
        {
            //change the extension as per your requirement
            if (Path.GetExtension(fileName3) == ".csv"//or .xlsx// .png // .jpg etc.
            {
                directories.Add(fileName3);
            }

            fileName3 = streamReader.ReadLine();

        }

        streamReader.Close();


        using (WebClient ftpClient = new WebClient())
        {
            ftpClient.Credentials = newSystem.Net.NetworkCredential(username , password );

            for (int i = 0; i <= directories.Count - 1; i++)
            {
                if (directories[i].Contains("."))
                {

                    string path = FtpServer + directories[i].ToString();
                    string trnsfrpth = localpath + directories[i].ToString();
                    ftpClient.DownloadFile(path, trnsfrpth);
                }
            }
        } 
       

    }

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !