Upload image path-name reads "System.Web.HttpPostedFileWrapper"

Go To StackoverFlow.com

2

Had this working; at one stage. The problem is the following text is now appearing in the field for the image in the database "System.Web.HttpPostedFileWrapper"

the following code is from the controller:

  [HttpPost]
    public ActionResult Create(CarAdvert caradvert,
         HttpPostedFileBase picture1)
    {
        if (ModelState.IsValid)
        {

            if (picture1 != null)
            {
                string image1 = picture1.FileName;
                caradvert.Image1 = image1;
                var image1Path = Path.Combine(Server.MapPath("~/Content/Images"), image1);
                picture1.SaveAs(image1Path);
            }


             db.CarAdverts.Add(caradvert);
             db.SaveChanges();
             return RedirectToAction("Index");  
        }

This code is from the create view:

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

    @Html.ValidationSummary(true)
    <fieldset>
        <legend>CarAdvert</legend>
      <div class="editor-field">

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

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
    }
2012-04-04 22:05
by Daza Aza


2

the paramater in the controller for HttpPostedFileBase has to have the same name as the input type="file" . Either do:

[HttpPost]     
public ActionResult Create(CarAdvert caradvert,          
HttpPostedFileBase Image1)

or

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

That should do it

2012-04-04 22:23
by MikeTWebb
Ads