2

I'm growing thin learving mvc3. I have the following code in my controller

    [HttpPost]
    public ActionResult Accreditation(Accreditation accreditation)
    {
        if (ModelState.IsValid)
        {
            var fileUpload = Request.Files[0];
            var uploadPath = Server.MapPath("~/App_Data/uploads");

            using (var fs = new FileStream(Path.Combine(uploadPath, accreditation.PressCard.ToString()), FileMode.Create))
            {
                var buffer = new byte[fileUpload.InputStream.Length];
                fileUpload.InputStream.Read(buffer, 0, buffer.Length);

                fs.Write(buffer, 0, buffer.Length);
            }

            context.Accreditations.Add(accreditation);
            context.SaveChanges();

            return RedirectToAction("Index");  
        }


           ViewBag.PossibleNationalities = context.Nationalities;
            ViewBag.PossibleNchis = context.Nchis;
            ViewBag.PossibleMedia = context.Media;
            ViewBag.PossibleEmploymentStatus = context.EmploymentStatus;
            return View(accreditation);



    }
}

Here's the View:

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

<div class="editor-field">
       <input type="file" name="PressCard" id="PressCard" data-val-required="Press card is required" data-val="true"/>
         @Html.ValidationMessageFor(model => model.PressCard)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Passport)
    </div>
    <div class="editor-field">
       <input type="file" name="Passport" id="Passport" data-val-required="ID/Passport is required" data-val="true"/>
        @Html.ValidationMessageFor(model => model.Passport)
    </div>

....... ........

When I try to upload, i get the following error message:

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Any one out there with a pointer to the right direction?


sorry for delay. Here's the new code

[HttpPost]
public ActionResult Accreditation(Accreditation accreditation, HttpPostedFileBase Passport)
    {
        if (ModelState.IsValid)
        {
            var uploadPath = Server.MapPath("~/App_Data/uploads");

            using (var fs = new FileStream(Path.Combine(uploadPath, accreditation.PressCard.ToString()), FileMode.Create))
            {
                var buffer = new byte[Passport.InputStream.Length];
                Passport.InputStream.Read(buffer, 0, buffer.Length);

                fs.Write(buffer, 0, buffer.Length);
            }

            context.Accreditations.Add(accreditation);
            context.SaveChanges();

            return RedirectToAction("Index");  
        }


           ViewBag.PossibleNationalities = context.Nationalities;
            ViewBag.PossibleNchis = context.Nchis;
            ViewBag.PossibleMedia = context.Media;
            ViewBag.PossibleEmploymentStatus = context.EmploymentStatus;
            return View(accreditation);



    }
}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Masinde Muliro
  • 1,175
  • 3
  • 24
  • 38

3 Answers3

4

Are really uploading data? I'd suggest you use this way. Create a parameter of type HttpPostedFileBase with the same name as the input field and test for its content length property.

Don't forget to use the same name for the parameter and for the input tag.

Checking this link will the fastest way for you to move on.

MVC 3 file upload and model binding

Community
  • 1
  • 1
Fabio Milheiro
  • 8,100
  • 17
  • 57
  • 96
  • Thanks for your quick feedback, means alot i changed the action to read [HttpPost] public ActionResult Accreditation(Accreditation accreditation, HttpPostedFileBase Passport) { } New error Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. – Masinde Muliro Sep 19 '11 at 13:59
  • @Edgar Ochieng, please, post the new code in the answer so we can have a look at it. – Fabio Milheiro Sep 19 '11 at 21:57
1

Get your posted file directly in action:

Here is discussion on SO: MVC 3 file upload and model binding

[HttpPost]
public ActionResult Accreditation(Accreditation accreditation, HttpPostedFileBase Passport)
{
    ...
}
Community
  • 1
  • 1
Samich
  • 29,157
  • 6
  • 68
  • 77
  • Thanks for your quick feedback, means alot i changed the action to read [HttpPost] public ActionResult Accreditation(Accreditation accreditation, HttpPostedFileBase Passport) { } New error Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. – Masinde Muliro Sep 19 '11 at 13:58
1

var fileUpload = Request.Files[0]; is the line where you have your exception, isn't it? You should expect the file to be stored in the property of class Accreditation, not in Request.Files.

So you need properties PressCard and Passport in Accreditation, both of type HttpPostedFileBase, and then use these properties in your code.

Michael Sagalovich
  • 2,539
  • 19
  • 26
  • Thanks for your quick feedback, means alot i changed the action to read [HttpPost] public ActionResult Accreditation(Accreditation accreditation, HttpPostedFileBase Passport) { } New error Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. – Masinde Muliro Sep 19 '11 at 13:59