Step - 1
In View :--
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<h3>Multiple file upload with asp.net mvc3, C# and HTML5 </h3>
<input type="file" name="files" value="" multiple="multiple"/>
<input type="submit" value="Upload You Image" title="Uplad"/>
<div style="color:Red;font-size:14px">@ViewBag.Message</div>
}
Step - 2
In .CS code section please see this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MultiplefileUploadinmvc3.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase[] files)
{
try
{
foreach (HttpPostedFileBase file in files)
{
//Geting the file name
string filename = System.IO.Path.GetFileName(file.FileName);
//Saving the file in server folder
file.SaveAs(Server.MapPath("~/Images/" + filename));
string filepathtosave = "Images/" + filename;
//--HERE WILL BE YOUR CODE TO SAVE THE FILE DETAIL IN DATA BASE--------------
}
ViewBag.Message = "File Uploaded successfully.";
}
catch
{
ViewBag.Message = "Error while uploading the files.";
}
return View();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MultiplefileUploadinmvc3.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase[] files)
{
try
{
foreach (HttpPostedFileBase file in files)
{
//Geting the file name
string filename = System.IO.Path.GetFileName(file.FileName);
//Saving the file in server folder
file.SaveAs(Server.MapPath("~/Images/" + filename));
string filepathtosave = "Images/" + filename;
//--HERE WILL BE YOUR CODE TO SAVE THE FILE DETAIL IN DATA BASE--------------
}
ViewBag.Message = "File Uploaded successfully.";
}
catch
{
ViewBag.Message = "Error while uploading the files.";
}
return View();
}
}
}
No comments:
Post a Comment