To achieve the desired functionality, one can easily utilize the jQuery Forms plugin. It can be seamlessly integrated into asp webforms, as demonstrated below.
HTML / ASPX Page
<form id="myForm" action="fileupload.ashx" method="post">
Name: <input type="text" name="name" />
File: <input type="file" name="filetoupload" />
<input type="submit" value="Submit Comment" />
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
$(document).ready(function () {
// attach handler to form's submit event
$('#myForm').submit(function () {
// submit the form
$(this).ajaxSubmit();
// return false to prevent normal browser submit and page navigation
return false;
});
});
</script>
This example showcases the use of pure HTML. In the case of aspx webforms, there will typically be only one form tag per page.
Include a WebHandler (.ashx extension) on the Website.
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
HttpPostedFile MyFile=context.Request.Files["filetoupload"];
if (MyFile.ContentLength > 0 && MyFile != null)
{
MyFile.SaveAs(context.Server.MapPath("Path/On/Server"));
}
context.Response.Write("Saved Successfully");
}
public bool IsReusable {
get {
return false;
}
}