Breaking News

Editors Picks

Monday, November 7, 2011

How to upload a file on FTP Server in ASP.Net


using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.IO;

///

/// Summary description for ftpuploadfile
///

public class ftpuploadfile
{
      public ftpuploadfile()
      {
            //
            // TODO: Add constructor logic here
            //
      }
    public void Upload(string path)
    {
        try
        {
            //WriteText("IN time for Upload file on FTP: " + DateTime.Now.ToString() + "");
            string ftpServerIP = "ftpservername".;//"120.0.0.2";
            // string filepath = path + "\\" + filename;
            FileInfo fileInf = new FileInfo(path);
            string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
            FtpWebRequest reqFTP;
            // Create FtpWebRequest object from the Uri provided
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
            // Provide the WebPermission Credintials
            reqFTP.Credentials = new NetworkCredential("", "");
            // By default KeepAlive is true, where the control connection is
            // not closed after a command is executed.
            reqFTP.KeepAlive = false;
            // Specify the command to be executed.
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            // Specify the data transfer type.
            reqFTP.UseBinary = true;
            // Notify the server about the size of the uploaded file
            reqFTP.ContentLength = fileInf.Length;
            // The buffer size is set to 2kb
            //bool exists = ftpFileExist(uri);

            if (ftpFileExist(uri) == false)
            {
                int buffLength = 2048;
                byte[] buff = new byte[10000];
                int contentLen;
                // Opens a file stream (System.IO.FileStream) to read
                //the file to be uploaded
                FileStream fs = fileInf.OpenRead();
                try
                {
                    //WriteText("IN time for file write on FTP: " + DateTime.Now.ToString() + "");
                    // Stream to which the file to be upload is written
                    Stream strm = reqFTP.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();
                    WriteText("Successfully file write on FTP:");
                    // WriteText("out time for file write on FTP: " + DateTime.Now.ToString() + "");
                    WriteText("-------------------------------------------------------------------------");
                }
                catch (Exception ex)
                {
                    WriteText("Error on file write on FTP:" + ex.Message + "");
                    WriteText("--------------------------------------------------------------------------");
                }
            }
            else
            {

                int buffLength = 2048;
                byte[] buff = new byte[10000];
                int contentLen;
                // Opens a file stream (System.IO.FileStream) to read
                //the file to be uploaded
                FileStream fs = fileInf.OpenRead();
                try
                {
                    //WriteText("IN time for file write on FTP: " + DateTime.Now.ToString() + "");
                    // Stream to which the file to be upload is written
                    Stream strm = reqFTP.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();
                    WriteText("Successfully file write on FTP:");
                    // WriteText("out time for file write on FTP: " + DateTime.Now.ToString() + "");
                    WriteText("-------------------------------------------------------------------------");
                }
                catch (Exception ex)
                {
                    WriteText("Error on file write on FTP:" + ex.Message + "");
                    WriteText("--------------------------------------------------------------------------");
                }
            }
            WriteText("Successfully Upload file on FTP:");         
            WriteText("-----------------------------------------------------------------------------------");
        }
        catch (Exception ex)
        {
            WriteText("Error on Upload file on FTP:" + ex.Message + "");         
            WriteText("--------------------------------------------------------------------------");
        }
    }
    public bool ftpFileExist(string fileName)
    {
        WebClient wc = new WebClient();
        try
        {
            wc.Credentials = new NetworkCredential("", "");
            byte[] fData = wc.DownloadData(fileName);
            if (fData.Length > -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception)
        {
            // Debug here?
        }
        return false;
    }
    // function for wtite logfile
    public void WriteText(string Message)
    {
        try
        {
            if (!string.IsNullOrEmpty(Message))
            {
                using (System.IO.StreamWriter w = System.IO.File.AppendText(ConfigurationManager.ConnectionStrings["ealogfile"].ToString()))
                {
                    w.WriteLine(Message);
                    w.Flush();
                    w.Close();
                }
            }
        }
        catch (Exception ex)
        {
            //System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
        }
    }
}
Read more ...

Contact Us

Name

Email *

Message *