Skip to content

Instantly share code, notes, and snippets.

@rionmonster
Created April 6, 2016 14:39
Show Gist options
  • Save rionmonster/40890d53ad857a57afb4836635f7d898 to your computer and use it in GitHub Desktop.
Save rionmonster/40890d53ad857a57afb4836635f7d898 to your computer and use it in GitHub Desktop.
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
if (file == null)
{
return Content("File was null.");
}
return Content($"File name is {file.FileName}.");
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>File Testing</title>
</head>
<body>
<h2>AJAX Form</h2>
@using (Ajax.BeginForm("Index", "Home", new AjaxOptions()
{
HttpMethod = "Post",
InsertionMode = InsertionMode.Replace,
}, new { enctype = "multipart/form-data" }))
{
<input name='file' type='file' />
<input type='submit'>
}
<h2>Normal Form</h2>
@using (Html.BeginForm("Index", "Home",FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input name='file' type='file' />
<input type='submit'>
}
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment