C# .NET - I want to Upload txt file,Vedio file on FTP Server

Asked By ALOK RANJAN on 01-May-12 08:56 AM
hi,

                      I want to Upload txt file,Vedio file on FTP Server. please provide me solution.











Thanks 
Alok





Sreekumar P replied to ALOK RANJAN on 01-May-12 09:20 AM
Hi,

You can upload any type of file to FTP, no matter what is the type of the file.
I can be txt file,Vedio file or anything else

This sample shows how to upload a file to an FTP server.

using System;
using System.IO;
using System.Net;
using System.Text;
 
namespace Examples.System.Net
{
  public class WebRequestGetExample
  {
    public static void Main ()
    {
      // Get the object used to communicate with the server.
      FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
      request.Method = WebRequestMethods.Ftp.UploadFile;
 
      // This example assumes the FTP site uses anonymous logon.
      request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
       
      // Copy the contents of the file to the request stream.
      StreamReader sourceStream = new StreamReader("testfile.txt");
      byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
      sourceStream.Close();
      request.ContentLength = fileContents.Length;
 
      Stream requestStream = request.GetRequestStream();
      requestStream.Write(fileContents, 0, fileContents.Length);
      requestStream.Close();
 
      FtpWebResponse response = (FtpWebResponse)request.GetResponse();
   
      Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
   
      response.Close();
      }
    }
  }
}


please mark helpful if it seems to be that
by,
Sreekumar P
bharti odedra replied to ALOK RANJAN on 01-May-12 09:36 AM

Video $ text file  upload

using System;

using System.IO;

using System.Net;

using System.Text;

 

namespace Examples.System.Net

{

  public class WebRequestGetExample

  {

  public static void Main ()

  {

    

    FtpWebRequest obs = (FtpWebRequest)WebRequest.Create("ftp://www.xyz.com/test.htm");

    obs.Method = WebRequestMethods.Ftp.UploadFile;

 

    

    obs.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

     

    

    StreamReader sourceStream = new StreamReader("testfile.txt");

    byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());

    sourceStream.Close();

    obs.ContentLength = fileContents.Length;

 

    Stream requestStream = obs.GetRequestStream();

    requestStream.Write(fileContents, 0, fileContents.Length);

    requestStream.Close();

 

    FtpWebResponse response = (FtpWebResponse) obs.GetResponse();

   

    Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

   

    response.Close();

    }

  }

  }

}

Jitendra Faye replied to ALOK RANJAN on 02-May-12 12:29 AM
Use this code-


String sourcefilepath = “@absolutepath”; // e.g. “d:/test.docx”
String ftpurl = “@ftpurl”; // e.g. ftp://serverip/foldername/foldername
String ftpusername = “@ftpusername”; // e.g. username
String ftppassword = “@ftppassword”; // e.g. password

private static void UploadFileToFTP(string source)
      {
        try
        {
          string filename = Path.GetFileName(source);
          string ftpfullpath = ftpurl;
          FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
          ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
 
          ftp.KeepAlive = true;
          ftp.UseBinary = true;
          ftp.Method = WebRequestMethods.Ftp.UploadFile;
          
          FileStream fs = File.OpenRead(source);
          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();
        }
        catch (Exception ex)
        {
          throw ex;
        }
      }

Try this and let me know.