Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

FtpWebRequest

0.00/5 (No votes)
19 Jan 2010 1  
FtpWebRequest class to upload a file to ftp server (destination )This Function shows how to upload a file ftp server. this function simply return

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

FtpWebRequest class to upload a file to ftp server (destination )


This Function shows how to upload a file ftp server. this function simply return true or false

 

if uploaded successfully it will return true else false

       public bool Upload(FileInfo fileInfo)
        {
        
            string uri = "";
            string serverIp = "Studentacad.com";
            string Username = "Aamir";
            string Password = "Hasan";

         
                uri = "ftp://" + serverIp + "/"  + fileInfo.Name;
      
            FtpWebRequest reqFTP;
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
          
            reqFTP.Credentials = new NetworkCredential(Username, Password);
            reqFTP.KeepAlive = false;
            reqFTP.Proxy = null;
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            reqFTP.UseBinary = true;
            reqFTP.Timeout = 8000;
            reqFTP.ContentLength = fileInfo.Length;
            int buffLength = 433664;//1048;// *16;
            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();
               
               return true;

            }
           
            catch (Exception ex)
            {
                if (reqFTP != null)
                {
                    reqFTP.Abort();
                }
              return false
           }
      }

 

 

http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx

http://www.asp.net/learn/videos/video-448.aspx

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here