Monday 6 July 2015

Files Upload in FTP Using C#

using System.IO;
using System.Net;

Path = Server.MapPath("~/Folder/file_Name.txt");

private void UploadToFTP(string Path)
    {
        FileInfo fileInfo = new FileInfo(Path);
        string uri = "ftp://000.000.000.00/folder/" + fileInfo.Name;
        FtpWebRequest reqFTP;

        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://000.000.000.00/folder/" + fileInfo.Name));
        reqFTP.Credentials = new NetworkCredential("userName""password");
        reqFTP.KeepAlive = false;
        reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
        reqFTP.UseBinary = true;
        reqFTP.UsePassive = false;
        reqFTP.ContentLength = fileInfo.Length;
        int buffLength = 10240;
        byte[] buff = new byte[buffLength];
        int contentLen;
        FileStream fs = fileInfo.OpenRead();
        try
        {
            Stream strm = reqFTP.GetRequestStream();
            contentLen = fs.Read(buff, 0, buffLength);
            while (contentLen != 0)
            {
                strm.Write(buff, 0, contentLen);
                contentLen = fs.Read(buff, 0, buffLength);
            }
            strm.Close();
            fs.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }

    }


No comments:

Post a Comment