Thursday, December 2, 2010

Send file on FTP using .net

To send a file over FTP using asp.net / c#.net / vb.net :

using System.Net;

public void ftpfile(string ftpfilepath, string inputfilepath)
    {

        string ftphost = "122.22.1.1"; // destination IP address      

        string ftpfullpath = "ftp://" + ftphost + ftpfilepath;

        FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
        ftp.Credentials = new NetworkCredential("userid", "password");
        ftp.KeepAlive = true;
        ftp.UseBinary = true;
        ftp.Method = WebRequestMethods.Ftp.UploadFile;
        FileStream fs = File.OpenRead(inputfilepath);
        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();

    }  

Solution by: Rajesh Rolen

Share This!


2 comments:

Anonymous said...

thanks for providing solution. keep it up.

Anonymous said...

this solution made me yours crazy fan...

Powered By Blogger · Designed By Seo Blogger Templates