I made a tutorial on it, you can see it here on my blog:
http://developmentpassion.blogspot.com/2013/08/aspnet-mvc-ajax-file-uploading-using.html
Here is the code:
@using (Html.BeginForm("Upload","TestDetails", FormMethod.Post,
new
{
enctype = "multipart/form-data",
id = "ImgForm",
name = "ImgForm",
target = "UploadTarget"
}))
{
<input type="hidden" id="bookingdid" name="bookingdid" value="@Model.BookingDId" />
<a class="close-reveal-modal"><img alt="close" src="@Url.Content("~/images/crossicon.png")" width="32" /></a>
<div style="width:100px; margin:auto;float:left;line-height:30px;font-weight:bold;">File</div>
<div style="width:200px;float:left;margin:auto;">
<input type="file" id="uploadFile" name="file" data-val="true" data-val-required="please select a file" />
<span id="validityMessages" style="color:Red;"></span>
</div>
<div style="clear:both;"></div>
<div style="width:auto; margin-top:3%;">
<div style="margin:5% 18%;">
<input id="upload" type="submit" value="Upload" name="Upload" onclick="UploadImage()" />
<span id="uploadLoader" style="display:none;"><img id="searchLoader" src="../Images/loader.gif" />Uploading Please Wait</span>
</div>
</div>
}
<div id="uploadsContainer"></div>
<iframe id="UploadTarget" name="UploadTarget" onload="UploadImage_Complete();" style="position: absolute; left: -999em; top: -999em;"></iframe>
</div>
My uploads class:
public class Uploads
{
public string FilePath { get; set; }
public long bookingdid { get; set; }
public string FileName { get; set; }
}
here is the action:
[HttpPost]
public ActionResult Upload(FormCollection form,HttpPostedFileBase file)
{
//HttpPostedFileBase file = Request.Files[0 ];
Uploads uploadsModel = new Uploads();
uploadsModel.bookingdid = long.Parse(form["bookingdid"]);
uploadsModel.FileName = file.FileName;
uploadsModel.FilePath = "~/Uploads/" + file.FileName;
if (file.ContentLength < 1048576)
{
if (file != null)
{
file.SaveAs(Server.MapPath(uploadsModel.FilePath));
}
TestDetailsViewModel objTestDetails = new TestDetailsViewModel();
objTestDetails.SaveFile(uploadsModel);
}
return PartialView("~/Views/Shared/_UploadsPartial.cshtml",uploadsModel);
}
Hope it helps