Please help me to fix my code why i can't use [HttpPost] in my action. Thanks a lot.
If not using [HttpPost], it work OK.
If using [HttpPost] => error show "The resource cannot be found."
My code bellow:
View:
@using (Html.BeginForm("Index", "ManageFiles", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<input type="file" id="UpFile" name="UpFile" /><input type="submit" value="Start upload" />
}
Controllers:
[HttpPost]
public ActionResult Index()
{
var path = "~/images/upload/";
// upload file
try
{
var upload = Request.Files["UpFile"];
if (upload != null && upload.ContentLength > 0)
{
upload.SaveAs(Server.MapPath(path + upload.FileName));
}
else
{
ModelState.AddModelError("", "The size of file must be between 0 and 2MB");
}
}
catch
{
ModelState.AddModelError("", "Maybe file size too large");
}
// end upload file
return View();
}