Ref:Upload and attach multiple files as attachments to email in ASP.Net
HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="Stylesheet" type="text/css" href="CSS/uploadify.css" />
<script type="text/javascript" src="scripts/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="scripts/jquery.uploadify.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div style="padding: 40px">
<asp:FileUpload ID="FileUpload1" runat="server" />
<table class="rounded_corners" id="attachedfiles">
</table>
</div>
</form>
</body>
</html>
Script:
<script type="text/javascript">
$(window).load(
function () {
$("#<%=FileUpload1.ClientID %>").fileUpload({
'uploader': 'scripts/uploader.swf',
'cancelImg': 'images/cancel.png',
'buttonText': 'Browse Files',
'script': 'Upload.ashx',
'folder': 'uploads',
'fileDesc': 'Image Files',
'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
'multi': true,
'auto': true,
'onComplete': function (event, ID, file, response, data) {
$("#attachedfiles").append("<tr><td>" + file.name + "</td><td><img src=" + file.filePath + " height='100' width='100' /></td><td><a href = 'javascript:;'>[x]</a></td></tr>");
}
});
}
);
</script>
<script type="text/javascript">
$("#attachedfiles a").live("click", function () {
var row = $(this).closest("tr");
var fileName = $("td", row).eq(0).html();
$.ajax({
type: "POST",
url: "AutoUpload.aspx/RemoveFile",
data: '{fileName: "' + fileName + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () { },
failure: function (response) {
alert(response.d);
}
});
row.remove();
});
</script>
C#:
[WebMethod]
public static void RemoveFile(string fileName)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM FilesTable WHERE FileName = @FileName ", con))
{
cmd.Parameters.AddWithValue("@FileName", fileName);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Style:
<style type="text/css">
.rounded_corners
{
border: 1px solid #A1DCF2;
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
border-radius: 8px;
overflow: hidden;
}
.rounded_corners td, .rounded_corners th
{
border: 1px solid #A1DCF2;
font-family: Arial;
font-size: 10pt;
text-align: center;
}
.rounded_corners table table td
{
border-style: none;
}
</style>
Handler:
FileName: Upload.ashx
public class Upload : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Expires = -1;
try
{
HttpPostedFile postedFile = context.Request.Files["Filedata"];
string savepath = "";
string tempPath = "";
tempPath = System.Configuration.ConfigurationManager.AppSettings["FolderPath"];
savepath = context.Server.MapPath(tempPath);
string filename = postedFile.FileName;
if (!Directory.Exists(savepath))
Directory.CreateDirectory(savepath);
postedFile.SaveAs(savepath + @"\" + filename);
context.Response.Write(tempPath + "/" + filename);
context.Response.StatusCode = 200;
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO FilesTable VALUES(@FileName,@FilePath)", con))
{
cmd.Parameters.AddWithValue("@FileName", filename);
cmd.Parameters.AddWithValue("@FilePath", tempPath + "/" + filename);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
catch (Exception ex)
{
context.Response.Write("Error: " + ex.Message);
}
}
public bool IsReusable
{
get
{
return false;
}
}
Image:

Thank You.