I'm new to ASP.Net (and webdev in general) and I'm trying to implement a site offering CV-like capabilities. One of such things would be to upload and view a CV photo of a person. Using scaffolding and a code sample found at SO: Uploading/Displaying Images in MVC 4 I managed to get as far as coming up with the following code where the problematic part is enclosed with a larger body of space:
<div class="form-horizontal">
<h4>Author</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.PersonID)
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Picture, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@using (Html.BeginForm("FileUpload", "CV", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" id="file" style="width: 100%;" />
<br/>
<input type="submit" value="Upload" class="submit" />
}
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.MobileNum, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.MobileNum, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MobileNum, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Location, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Location, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Location, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LinkedIn, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LinkedIn, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LinkedIn, "", new { @class = "text-danger" })
</div>
</div>
which is the relevant part of the view responsible for uploading, while the method in the Controller
CV that I want to handle the request goes like this:
[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase file)
{
if (file != null)
{
string pic = System.IO.Path.GetFileName(file.FileName);
string path = System.IO.Path.Combine(
Server.MapPath("~/Content/Images"), pic);
// file is uploaded
file.SaveAs(path);
// save the image path path to the database or you can send image
// directly to database
// in-case if you want to store byte[] ie. for DB
using (MemoryStream ms = new MemoryStream())
{
file.InputStream.CopyTo(ms);
byte[] array = ms.GetBuffer();
//now I only have 1 mock author
db.Authors.FirstOrDefault().Picture = array;
}
}
// after successfully uploading redirect the user
return View();
}
My problem is that the post never hits this method for some reason despite having specified both the Action
and the Controller
name and VS being capable of connecting the View
to the Controller
(at least it swipes to the appropriate one on the Go To Controller
command). I'm sure it's some rookie mistake, but I just can't seem to find the cause.