HI Waghmare,
Refer below sample.
HTML
<asp:FileUpload ID="fuUpload" runat="server" />
<asp:Button Text="Upload" runat="server" OnClick="OnUpload" />
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Net;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.IO
Imports System.Net
Code
C#
protected void OnUpload(object sender, EventArgs e)
{
string serverName = "//yourserver.com/";
string folder = "Uploads/";
string fileName = Path.GetFileName(fuUpload.FileName);
// Upload to current server.
fuUpload.PostedFile.SaveAs(Server.MapPath("~/" + folder + fileName));
// Save path in database.
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
string sql = "INSERT INTO Files VALUES(@Name, @Path)";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@Name", fileName);
cmd.Parameters.AddWithValue("@Path", folder + fileName);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
// Upload to Hosted Server.
WebClient webClient = new WebClient();
webClient.Credentials = new NetworkCredential("UserName", "Password"); ;
Uri uri = new Uri(serverName + folder + fileName);
webClient.UploadFile(uri, "POST", fileName);
}
VB.Net
Protected Sub OnUpload(ByVal sender As Object, ByVal e As EventArgs)
Dim serverName As String = "//yourserver.com/"
Dim folder As String = "Uploads/"
Dim fileName As String = Path.GetFileName(fuUpload.FileName)
' Upload to current server.
fuUpload.PostedFile.SaveAs(Server.MapPath("~/" & folder & fileName))
' Save path in database.
Using conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Dim sql As String = "INSERT INTO Files VALUES(@Name, @Path)"
Using cmd As SqlCommand = New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("@Name", fileName)
cmd.Parameters.AddWithValue("@Path", folder & fileName)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End Using
End Using
' Upload to Hosted Server.
Dim webClient As WebClient = New WebClient()
webClient.Credentials = New NetworkCredential("UserName", "Password")
Dim uri As Uri = New Uri(serverName & folder & fileName)
webClient.UploadFile(uri, "POST", fileName)
End Sub