Upload Files to FTP

File Uploading from client to FTP.

        private void UploadFileToFtp(string ftpServer, string ftpUser, string ftpPass, string filename,List<string> files, out string strMsg)

        {           

            strMsg = string.Empty;

            foreach (string file in files)

            {

                System.IO.FileInfo fileInf = new System.IO.FileInfo(file);

                FtpWebRequest reqFTP;

 

                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(string.Format("{0}/{1}", ftpServer, fileInf.Name)));

                reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPass);

 

                reqFTP.KeepAlive = false;

                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

                reqFTP.UseBinary = true;

                reqFTP.ContentLength = fileInf.Length;

 

                int buffLength = 20480;

                byte[] buff = new byte[buffLength];

                int contentLen;

 

                System.IO.FileStream fs = fileInf.OpenRead();

 

                try

                {

                    System.IO.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();

                    strMsg = "Upload Successful ";

                }

                catch (Exception ex)

                {

                    strMsg += string.Format("Ftp Upload Error for file :{0} and Error is !: {1}",file,ex.Message);

                }

            }

        }