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 upload file to 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:33

Introduction:
Some of your programming requirement may be to upload 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 Upload(string localFilename, string targetFilename)

        {


            FileInfo fi = new FileInfo(localFilename);//Full path of the local file

           


          

            string target;

            // targetFilename file can be full path or just filename (uses current dir)

            if (targetFilename.Trim() == "")

            {

              

                target = this.CurrentDirectory + fi.Name;

            }

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

            {

              

                target = AdjustDir(targetFilename);

            }

            else

            {

              

                target = CurrentDirectory + targetFilename;

            }


            string URI = Hostname + target;

          

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


           

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

            ftp.UseBinary = true;


            //Notify FTP of the expected size

            ftp.ContentLength = fi.Length;


            //create byte array to store: ensure at least 1 byte!

            const int BufferSize = 2048;

            byte[] content = new byte[BufferSize - 1 + 1];

            int dataRead;


            //open file for reading

            using (FileStream fs = fi.OpenRead())

            {

                try

                {

                    //open request to send

                    using (Stream rs = ftp.GetRequestStream())

                    {

                        do

                        {

                            dataRead = fs.Read(content, 0, BufferSize);

                            rs.Write(content, 0, dataRead);

                        } while (!(dataRead < BufferSize));

                        rs.Close();

                    }


                }

                catch (Exception ex)

                {


                }

                finally

                {

                   

                    fs.Close();

                }


            }




            ftp = null;

            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 upload 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)