Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Sunday, June 10, 2012
File Upload Asp.net Mvc3.0
I want to upload an excel file in asp.net mvc3.0. How can I upload the file using html input file control.
You don't use a file input control. Server side controls are not used in ASP.NET MVC. Checkout the following blog post which illustrates how to achieve this in ASP.NET MVC.
So you would start by creating a an HTML form which would contain a file input:
and then you would have a controller to handle the upload:
public class HomeController : Controller { // This action renders the form public ActionResult Index() { return View(); }
// This action handles the form POST and the upload [HttpPost] public ActionResult Index(HttpPostedFileBase file) { // Verify that the user selected a file if (file != null && file.ContentLength > 0) { // extract only the fielname var fileName = Path.GetFileName(file.FileName); // store the file inside ~/App_Data/uploads folder var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName); file.SaveAs(path); } // redirect back to the index action to show the form once again return RedirectToAction("Index"); } }
Below is the method to upload a file: form action="" method="post" enctype="multipart/form-data" label for="file">Filename:</label input type="file" name="file" id="file" / input type="submit" / /form
You don't use a file input control. Server side controls are not used in ASP.NET MVC. Checkout the following blog post which illustrates how to achieve this in ASP.NET MVC.
ReplyDeleteSo you would start by creating a an HTML form which would contain a file input:
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<input type="submit" value="OK" />
}
and then you would have a controller to handle the upload:
public class HomeController : Controller
{
// This action renders the form
public ActionResult Index()
{
return View();
}
// This action handles the form POST and the upload
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
// Verify that the user selected a file
if (file != null && file.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
// redirect back to the index action to show the form once again
return RedirectToAction("Index");
}
}
to transfer to byte[] (e.g. for saving to DB):
ReplyDeleteusing (MemoryStream ms = new MemoryStream()) {
file.InputStream.CopyTo(ms);
byte[] array = ms.GetBuffer();
}
Below is the method to upload a file:
ReplyDeleteform action="" method="post" enctype="multipart/form-data"
label for="file">Filename:</label
input type="file" name="file" id="file" /
input type="submit" /
/form