Hi eshant.kapoor...,
Use Generic Handler to upload the files using FormData and jQuery Ajax.
Refer below example.
HTML
<div>
<input type="file" name="fuUpload" id="fuUpload" multiple="multiple" />
<input name="txtName" type="text" id="txtName" />
<asp:Button ID="btnSave" runat="server" Text="Save" value="Save" />
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('#btnSave').on("click", function () {
var formData = new FormData();
for (var i = 0; i < $("#fuUpload").prop("files").length; i++) {
formData.append($("#fuUpload").prop("files")[i].name, $("#fuUpload").prop("files")[i]);
}
formData.append("Name", $('#txtName').val());
$.ajax({
type: 'POST',
url: "Handler.ashx",
data: formData,
cache: false,
contentType: false,
processData: false,
success: function (r) {
alert(r.d);
},
error: function (r) {
alert(r.responseText);
}
})
return false;
});
});
</script>
Handler
C#
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.Request.Files.Count > 0)
{
string name = context.Request["Name"];
foreach (HttpPostedFile file in context.Request.Files)
{
file.SaveAs(HttpContext.Current.Server.MapPath("~/Files/" + Path.GetFileName(file.FileName)));
SaveDetails(name, "~/Files/" + Path.GetFileName(file.FileName), Path.GetFileName(file.FileName));
}
}
}
private static void SaveDetails(string name, string filePath, string fileName)
{
string str = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
using (SqlConnection con = new SqlConnection(str))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("Insert INTO Files (Name,FilePath,FileName) values (@Name,@FilePath,@FileName)", con))
{
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@FilePath", filePath);
cmd.Parameters.AddWithValue("@FileName", fileName);
cmd.ExecuteNonQuery();
}
con.Close();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
VB.Net
<%@ WebHandler Language="VB" Class="Handler" %>
Imports System
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.IO
Public Class Handler : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
If context.Request.Files.Count > 0 Then
Dim name As String = context.Request("Name")
For Each file As HttpPostedFile In context.Request.Files
file.SaveAs(HttpContext.Current.Server.MapPath("~/Files/" & Path.GetFileName(file.FileName)))
SaveDetails(name, "~/Files/" & Path.GetFileName(file.FileName), Path.GetFileName(file.FileName))
Next
End If
End Sub
Private Shared Sub SaveDetails(ByVal name As String, ByVal filePath As String, ByVal fileName As String)
Dim str As String = ConfigurationManager.ConnectionStrings("conn").ConnectionString
Using con As SqlConnection = New SqlConnection(str)
con.Open()
Using cmd As SqlCommand = New SqlCommand("Insert INTO Files (Name,FilePath,FileName) values (@Name,@FilePath,@FileName)", con)
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@FilePath", filePath)
cmd.Parameters.AddWithValue("@FileName", fileName)
cmd.ExecuteNonQuery()
End Using
con.Close()
End Using
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class