I don't know what I'm missing, but I need to upload a file using C# MVC 3. I followed instructions here in SO, but the file is always empty.
Here is my actual testing code:
HTML
@using (Html.BeginForm("Prc", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" id="file" />
<input type="submit" value="submit" />
}
Controller
[HttpPost]
public ActionResult Prc(HttpPostedFile file)
{
if (file != null && file.ContentLength > 0)
{
var filename = System.IO.Path.GetFileName(file.FileName);
var path = System.IO.Path.Combine(Server.MapPath("~/Content/Images"), filename);
file.SaveAs(path);
}
return RedirectToAction("Index");
}
When I run the web app, I attach a file, and click Submit. But when I reach the Controller
, the file
object is null
. Always null
. I tried an XML
file, a JPEG
file, and a GIF
file but none of them worked.
Should I configure something else besides just these codes?
Thanks