Created
January 16, 2014 09:58
-
-
Save jackinf/8452458 to your computer and use it in GitHub Desktop.
File Uploader for ASP .NET MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) | |
{ | |
<input type="file" name="file" /> | |
<input type="submit" value="OK" /> | |
} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment