Upload File to FTP Server Using Asp.Net File Uploader


  • Using Ftp Web Request

System.Net.FtpWebRequest rq = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(@"ftp://XYZ.com/a1.temp");
rq.Credentials = new System.Net.NetworkCredential("username", "******");
rq.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
System.IO.Stream fs = FileUpload2.PostedFile.InputStream;
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
System.IO.
Stream ftpstream = rq.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
}
catch (Exception ei)
{
Label1.Text = ei.Message;
}

  • Using WebClient

try


{


     string location = "ad/" + FileUpload1.FileName;
     FileUpload1.PostedFile.SaveAs(Server.MapPath(location));
     System.Net.
    WebClient wc = new System.Net.WebClient();
    wc.Credentials =     new System.Net.NetworkCredential("hello", "******");
    wc.UploadFile(    new Uri(@"ftp://xyz.com/test.no"), Server.MapPath(location));
}catch (Exception ei)
{
   Label1.Text = ei.Message;
}