Hi simflex,
Refer below sample.
HTML
<script type="text/javascript" src="scripts/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="scripts/jquery.uploadify.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=FileUpload1]").fileUpload({
'uploader': 'scripts/uploader.swf',
'cancelImg': 'images/cancel.png',
'buttonText': 'Attach Files',
'script': 'UploadCS.ashx',
'folder': 'uploads',
'multi': true,
'auto': true,
'scriptData': { key: '<%=Key %>' },
'onSelect': function (event, ID, file) {
$("#attachedfiles tr").each(function () {
if ($("td", this).eq(0).html() == file.name) {
alert(file.name + " already uploaded.");
$("[id*=FileUpload1]").fileUploadCancel(ID);
return;
}
});
},
'onComplete': function (event, ID, file, response, data) {
$("#attachedfiles").append("<tr><td>" + file.name + "</td><td><a href = 'javascript:;'>[x]</a></td></tr>");
}
});
});
$("#attachedfiles a").live("click", function () {
var row = $(this).closest("tr");
var fileName = $("td", row).eq(0).html();
$.ajax({
type: "POST",
url: "CS.aspx/RemoveFile",
data: '{fileName: "' + fileName + '", key: "<%=Key %>" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () { },
failure: function (response) {
alert(response.d);
}
});
row.remove();
});
</script>
<table>
<tr>
<td>
To:
</td>
<td>
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Body:
</td>
<td>
<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<table id="attachedfiles">
</table>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" />
</td>
</tr>
</table>
Namespaces
C#
using System.Web.Services;
using System.Net.Mail;
using System.IO;
Code
UploadCS.ashx
<%@ WebHandler Language="C#" Class="UploadCS" %>
using System;
using System.Web;
using System.IO;
using System.Collections.Generic;
public class UploadCS : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Expires = -1;
try
{
List<HttpPostedFile> files = (List<HttpPostedFile>)context.Cache[context.Request.QueryString["key"]];
HttpPostedFile postedFile = context.Request.Files["Filedata"];
files.Add(postedFile);
string filename = postedFile.FileName;
postedFile.SaveAs(HttpContext.Current.Server.MapPath("~/Uploads/") + filename);
context.Response.Write(filename);
context.Response.StatusCode = 200;
}
catch (Exception ex)
{
context.Response.Write("Error: " + ex.Message);
}
}
public bool IsReusable {
get {
return false;
}
}
}
CS.aspx.cs
protected string Key
{
get { return ViewState["Key"].ToString(); }
set { ViewState["Key"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.Key = Guid.NewGuid().ToString();
Cache[this.Key] = new List<HttpPostedFile>();
}
}
[WebMethod]
public static void RemoveFile(string fileName, string key)
{
List<HttpPostedFile> files = (List<HttpPostedFile>)HttpContext.Current.Cache[key];
File.Delete(HttpContext.Current.Server.MapPath("~/Uploads/") + fileName.ToLower());
}