+ Reply to Thread
Results 1 to 2 of 2

Thread: Ftp upload/download

  1. #1

    Ftp upload/download

    I was hoping that someone show me a snippet of FTP file upload and download.

    Many thanks in advance.
    Last edited by csharpmaster; 11-01-2012 at 02:03 PM.

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    45
    About FTP [File Transfer Protocol] :
    FTP can be used to transfer files from pc to server or server to pc , so if you have got a server and want to
    upload files via FTP in c# , how would you do that ?
    Let`s assume first that you have created a FTP account on server , so you gonna use the FTP Host name [usually >> ftp.yourdomain.com]
    and FTP username & Password.


    FTP Upload :
    ------------
    Code:
            private void Upload(string filename)
            {
                FileInfo fi = new FileInfo(filename);
    
                FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("ftp://" + textBox1.Text + "/" + Path.GetFileName(filename));
                ftp.Credentials = new NetworkCredential(textBox2.Text, textBox3.Text);
                ftp.Method = WebRequestMethods.Ftp.UploadFile;
                ftp.UseBinary = true;
                ftp.KeepAlive = false;
                ftp.ContentLength = fi.Length;
    
                // The buffer size is set to 256kb
                int buffLength = 262144;
                byte[] buff = new byte[buffLength];
                int contentLen;
    
                FileStream fs = fi.OpenRead();
    
                try
                {
                    // Stream to which the file to be upload is written
                    Stream strm = ftp.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();
    				
    				// done uploading
                }
                catch (Exception ex)
                {
                    // error message goes here
                }
            }
    FTP Download :
    --------------
    Code:
    		// made in console application
            private void Download_FTP()
            {
                // Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.domain.com/file.exe");
                request.Method = WebRequestMethods.Ftp.DownloadFile;
    
                // This example assumes the FTP site uses anonymous logon.
                request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
    
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                Console.WriteLine(reader.ReadToEnd());
    
                Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
        
                reader.Close();
                response.Close();  
            }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts