Free Jobs
Reference




Dot Net Forums: ASPNET (77) C#.NET (39) VB.NET (8) AJAX (21) Silver Light (1) WCF (10) WPF (1) WWF (1) .NET 3.5 (3) SQL SERVER (38) Other (3)

Topic: How to download file from ftp server using C# ASP.NET

To get the Posted Questions details daily in your mail Click Here

Share this Post on Facebook/Twitter/Orkut Bookmark and Share





User: rajiv100 Post Date: 03 May 2011 06:42

Introduction:
Some of your programming requirement may be to download file to ftp server using C# ASP.NET.In this post we will be discuusing the same.



Main Content:


Add the using given below


using System.Net;

using System.IO;

using System.Text.RegularExpressions;



public bool Download(string sourceFilename, string localFilename, bool PermitOverwrite)

        {

           


            FileInfo targetFI = new FileInfo(localFilename);//localFilename can be file name or full path


            if (targetFI.Exists && !(PermitOverwrite))

            {

                throw (new ApplicationException("Target file already exists"));

            }


          

            string target;

            if (sourceFilename.Trim() == "")

            {

                throw (new ApplicationException("File not specified"));

            }

            else if (sourceFilename.Contains("/"))

            {

                //treat as a full path

                target = AdjustDir(sourceFilename);

            }

            else

            {

                //treat as filename only, use current directory

                target = CurrentDirectory + sourceFilename;

            }


            string URI = Hostname + target;


          

            System.Net.FtpWebRequest ftp = GetRequest(URI);


          

            ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;

            ftp.UseBinary = true;


           

            using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())

            {

                using (Stream responseStream = response.GetResponseStream())

                {

                  

                    using (FileStream fs = targetFI.OpenWrite())

                    {

                        try

                        {

                            byte[] buffer = new byte[2048];

                            int read = 0;

                            do

                            {

                                read = responseStream.Read(buffer, 0, buffer.Length);

                                fs.Write(buffer, 0, read);

                            } while (!(read == 0));

                            responseStream.Close();

                            fs.Flush();

                            fs.Close();

                        }

                        catch (Exception)

                        {

                          

                            fs.Close();

                            //delete target file as it''s incomplete

                            targetFI.Delete();

                            throw;

                        }

                    }


                    responseStream.Close();

                }


                response.Close();

            }




            return true;

        }




       private FtpWebRequest GetRequest(string URI)

        {

          

            FtpWebRequest result = (FtpWebRequest)FtpWebRequest.Create(URI);           

            result.Credentials = GetCredentials();

            //Do not keep alive (stateless mode)

            result.KeepAlive = false;

            return result;

        }


       private System.Net.ICredentials GetCredentials()

        {

            return new System.Net.NetworkCredential(Username, Password);

        }

       

        private string AdjustDir(string path)

        {

            return ((path.StartsWith("/")) ? "" : "/").ToString() + path;

        }


Conclusion:
Thus if we know host name ,username ,password we can download file to ftp.



About Writer:
Hi,I am .NET Developer From Bangaloret


Writer's Link:

Writer's Email:







Comments:
Post Comment





Similar Articles Top ArticlesLatest Articles Other Articles by this Author







www.freejobsreference.com Copyright © 2009-2011 | All Rights Reserved
All content of this website is protected by copyright laws and should not be reproduced in any form or by any means without written permission
(Best viewed in IE 8.0+ or Firefox 3.0+ at resolution of 1024 * 768 or higher)