Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having trouble with ASP.NET MVC 4 image uploading

I'm trying to upload a image along with other fields. Been following examples from here and here

However it seems that the image object isn't passed to the controller and i'm unable to find any error.

Here's the view:

@model Project.Models.ContentNode
@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm("Create", "News", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>News</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)
        </div>
        @*Image upload field*@
        <div class="editor-field">
            <input type="file" name="file" />
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Body)
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Body)
            @Html.ValidationMessageFor(model => model.Body)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

Here are the controller methods:

public ActionResult Create()
        {
            var model = new ContentNode();
            return View( model );
        }

        [HttpPost]
        public ActionResult Create(ContentNode nyF, HttpPostedFileBase imageData)
        {
            // Get the type of content from DB
            var k = (from ct in _db.ContentTypes
                     where ct.ID == 1
                     select ct).Single();

            var curUser = (from u in _db.Users
                           where u.Username == User.Identity.Name
                           select u).Single();

            nyF.Author = curUser;
            nyF.ContentType = k;
            nyF.dateCreated = DateTime.Now;

            _db.ContentNodes.Add(nyF);
            _db.SaveChanges();

            // Process image
            if ((imageData != null && imageData.ContentLength > 0))
            {
                var fileName = Path.GetFileName(imageData.FileName);
                var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                imageData.SaveAs(path);
            }
            else
            {
                return Content("The image object wasn't received");
            }

            return View(nyF);
        }

I've read over the whole thing over and over and am unable to see the error. Anyone here willing to point out what I'm doing wrong?

like image 270
grimurd Avatar asked Jan 29 '26 01:01

grimurd


1 Answers

The name of the input file needs to match the action parameter

<input type="file" name="file" />

should be

<input type="file" name="imageData" />

or you change your action parameter name

public ActionResult Create(ContentNode nyF, HttpPostedFileBase file)
like image 191
codingbiz Avatar answered Jan 30 '26 22:01

codingbiz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!