Thursday, July 22, 2010

How to uplaod a file to a ftp server

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net;

namespace WebApplication1.FTP
{
    public class FTPManager
    {
        public string FtpFilePath { get; set; }
        public string FtpInputFilePath { get; set; }
        public string FtpHost { get; set; }
        public string FtpUserName { get; set; }
        public string FtpPAssword { get; set; }

        public void UploadFile(FTPManager fm)
        {
            //try
            //{
            //string ftphost = "203.143.11.98";
            //here correct hostname or IP of the ftp server to be given 

            string ftpfullpath = "ftp://" + fm.FtpHost + fm.FtpFilePath;
            FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
            ftp.Credentials = new NetworkCredential(fm.FtpUserName, fm.FtpPAssword);
            //userid and password for the ftp server to given 

            ftp.KeepAlive = true;
            ftp.UseBinary = true;
            ftp.Method = WebRequestMethods.Ftp.UploadFile;


            FileStream fs = File.OpenRead(fm.FtpInputFilePath);

            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            int contentLen;



            try
            {
                // Stream to which the file to be upload is written
                Stream strm = ftp.GetRequestStream();

                // Read from the file stream 2kb at a time
                contentLen = fs.Read(buff, 0, buffLength);

                // Till Stream content ends
                while (contentLen != 0)
                {
                    // Write Content from the file stream to the FTP Upload
                    // Stream
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                }

                // Close the file stream and the Request Stream
                strm.Close();
                fs.Close();


            }
            catch
            {
                throw new WebException();
            }
        }

        public void UploadFile(FTPManager fm,byte[] buffer)
        {
            try
            {
                //string ftphost = "203.143.11.98";
                //here correct hostname or IP of the ftp server to be given 

                string ftpfullpath = "ftp://" + fm.FtpHost + fm.FtpFilePath;
                FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
                ftp.Credentials = new NetworkCredential(fm.FtpUserName, fm.FtpPAssword);
                //userid and password for the ftp server to given 

                ftp.KeepAlive = true;
                ftp.UseBinary = true;
                ftp.Method = WebRequestMethods.Ftp.UploadFile;
                //FileStream fs = File.OpenRead(fm.FtpInputFilePath);
                //byte[] buffer = new byte[fs.Length];
                //fs.Read(buffer, 0, buffer.Length);
                //fs.Close();
                Stream ftpstream = ftp.GetRequestStream();
                ftpstream.Write(buffer, 0, buffer.Length);
                ftpstream.Close();
            }
            catch
            {
                throw new InvalidDataException();
            }
        }
    }
}


This is the way to use this class

 FTP.FTPManager fm = new FTP.FTPManager();
            fm.FtpUserName = "com";
            fm.FtpPAssword = "Com123$";
            fm.FtpInputFilePath = @"D:/from desktop 2010-06-08.zip";
            fm.FtpFilePath = @"/TestFtp/from desktop 2010-06-08.zip";
            fm.FtpHost = "203.143.11.98";

            if (FileUpload1.HasFile)
            {
                fm.FtpFilePath = @"/TestFtp/"+FileUpload1.PostedFile.FileName;
                fm.UploadFile(fm, FileUpload1.FileBytes);             
            }
            else
                fm.UploadFile(fm);

No comments: