Hi namojainashis...,
Refer below code and modify as per your requirement and control id.
Change folder path according to your need.
HTML
<asp:TextBox runat="server" ID="txtName" />
<asp:TextBox runat="server" ID="txtCity" />
<asp:TextBox runat="server" ID="txtCountry" />
<asp:FileUpload runat="server" ID="FileUpload1" />
<asp:Button Text="Save" runat="server" OnClick="OnSave" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Text;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Net
Imports System.Text
Code
C#
protected void OnSave(object sender, EventArgs e)
{
string ftp = "ftp://yourserver.com/";
string userName = "tnd/WDevPMISftp1";
string password = "Pa$$word@123456";
string ftpFolder = "UploadToDMS/";
byte[] fileBytes = null;
string fileName = Path.GetFileName(FileUpload1.FileName);
using (StreamReader fileStream = new StreamReader(FileUpload1.PostedFile.InputStream))
{
fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd());
fileStream.Close();
}
// Upload FileUpload to FTP.
UploadToFTP(ftp, userName, password, ftpFolder, fileName, fileBytes);
// Insert into Database.
string query = "INSERT INTO Table VALUES(@Name,@City @Country@Path)";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@City", txtCity.Text);
cmd.Parameters.AddWithValue("@Country", txtCountry.Text);
cmd.Parameters.AddWithValue("@Path", ftp + ftpFolder + fileName);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
DataSet ds = new DataSet("Customers");
DataTable dt = new DataTable("Customer");
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Name"), new DataColumn("City"), new DataColumn("Country"), new DataColumn("FilePath") });
dt.Rows.Add(txtName.Text, txtCity.Text, txtCountry.Text, ftp + ftpFolder + fileName);
ds.Tables.Add(dt);
ds.WriteXml(Server.MapPath("~/SavedData.xml"));
// Upload XML File to FTP.
fileBytes = File.ReadAllBytes(Server.MapPath("~/SavedData.xml"));
UploadToFTP(ftp, userName, password, ftpFolder, "SavedData.xml", fileBytes);
}
private void UploadToFTP(string ftp, string userName, string password, string ftpFolder, string fileName, byte[] fileBytes)
{
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
//Enter FTP Server credentials.
request.Credentials = new NetworkCredential(userName, password);
request.ContentLength = fileBytes.Length;
request.UsePassive = true;
request.UseBinary = true;
request.ServicePoint.ConnectionLimit = fileBytes.Length;
request.EnableSsl = false;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(fileBytes, 0, fileBytes.Length);
requestStream.Close();
}
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
}
VB.Net
Protected Sub OnSave(ByVal sender As Object, ByVal e As EventArgs)
Dim ftp As String = "ftp://yourserver.com/"
Dim userName As String = "tnd/WDevPMISftp1"
Dim password As String = "Pa$$word@123456"
Dim ftpFolder As String = "UploadToDMS/"
Dim fileBytes As Byte() = Nothing
Dim fileName As String = Path.GetFileName(FileUpload1.FileName)
Using fileStream As StreamReader = New StreamReader(FileUpload1.PostedFile.InputStream)
fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd())
fileStream.Close()
End Using
' Upload FileUpload to FTP.
UploadToFTP(ftp, userName, password, ftpFolder, fileName, fileBytes)
' Insert into Database.
Dim query As String = "INSERT INTO Table VALUES(@Name,@City @Country@Path)"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@City", txtCity.Text)
cmd.Parameters.AddWithValue("@Country", txtCountry.Text)
cmd.Parameters.AddWithValue("@Path", ftp & ftpFolder & fileName)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Dim ds As DataSet = New DataSet("Customers")
Dim dt As DataTable = New DataTable("Customer")
dt.Columns.AddRange(New DataColumn() {New DataColumn("Name"), New DataColumn("City"), New DataColumn("Country"), New DataColumn("FilePath")})
dt.Rows.Add(txtName.Text, txtCity.Text, txtCountry.Text, ftp & ftpFolder & fileName)
ds.Tables.Add(dt)
ds.WriteXml(Server.MapPath("~/SavedData.xml"))
fileBytes = File.ReadAllBytes(Server.MapPath("~/SavedData.xml"))
' Upload XML File to FTP.
UploadToFTP(ftp, userName, password, ftpFolder, "SavedData.xml", fileBytes)
End Sub
Private Sub UploadToFTP(ByVal ftp As String, ByVal userName As String, ByVal password As String, ByVal ftpFolder As String, ByVal fileName As String, ByVal fileBytes As Byte())
Try
Dim request As FtpWebRequest = CType(WebRequest.Create(ftp & ftpFolder & fileName), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.UploadFile
request.Credentials = New NetworkCredential(userName, password)
request.ContentLength = fileBytes.Length
request.UsePassive = True
request.UseBinary = True
request.ServicePoint.ConnectionLimit = fileBytes.Length
request.EnableSsl = False
Using requestStream As Stream = request.GetRequestStream()
requestStream.Write(fileBytes, 0, fileBytes.Length)
requestStream.Close()
End Using
Dim response As FtpWebResponse = CType(request.GetResponse(), FtpWebResponse)
response.Close()
Catch ex As WebException
Throw New Exception((TryCast(ex.Response, FtpWebResponse)).StatusDescription)
End Try
End Sub