0

I have a form that has input fields and file upload field, I think that I have done everything but I have a problem with the binary reader. Model:

public class Event
{
    [Key]
    public int Id { get; set; }
    public string EventName { get; set; }
    public byte[] EventPhoto { get; set; }
}

Controller:

[Authorize]
[HttpPost]
public ActionResult Create(Event events, [Bind(Exclude = "EventPhoto")]EventController model)
{
    if (ModelState.IsValid)
    {
        using (var database = new EventSpotDbContext())
        {
            byte[] imageData = null;
            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase poImgFile = Request.Files["EventPhoto"];

                using (var binary = new BinaryReader(poImgFile.InputStream))
                {
                    imageData = binary.ReadBytes(poImgFile.ContentLength);
                }
            }
            events.EventPhoto = imageData;
            database.Events.Add(events);
            database.SaveChanges();
            return RedirectToAction("Main");
        }
    }
    return View(events);
}

View:

@using (Html.BeginForm("Create", "Event", FormMethod.Post, new { @class = "form-horizontal" , role = "form", enctype = "multipart/form-data" } ))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary("", new { @class = "text-danger" })

    @Html.LabelFor(m => m.EventName)
    @Html.TextBoxFor(m => m.EventName)

    @Html.LabelFor(m => m.EventPhoto)
   <input type="file" name="Event" id="fileUpload" accept=".png,.jpg,.jpeg,.gif,.tif" />

   <input type="submit" value="Create" class="btn btn-success" />
}

On post Error:

Server Error in '/' Application.

Object reference not set to an instance of an object. NullReferenceException: Object reference not set to an instance of an object.

Line 83: using (var binary = new >BinaryReader(poImgFile.InputStream))

  • 1
    Check 2nd answer. Possible duplicate of [File Upload as Part of Form with Other Fields](http://stackoverflow.com/questions/17128196/file-upload-as-part-of-form-with-other-fields) – Mate Dec 17 '16 at 23:27
  • What problem? Doesn't compile? What is an error? – Piotr M Dec 17 '16 at 23:27
  • What problem? What are you expecting to happen. What actually happens? [How do I ask a good question?](http://stackoverflow.com/help/asking) –  Dec 17 '16 at 23:28
  • Server Error in '/' ApplicationObject reference not set to an instance of an object.. Source Error: Line 92: HttpPostedFileBase poImgFile = Request.Files["EventPhoto"]; Line 93: Line 94: using (var binary = new BinaryReader(poImgFile.InputStream)) Line 95: { Line 96: imageData = binary.ReadBytes(poImgFile.ContentLength); It needs to save the image in the DB in a column in Events table – ivanov.g94 Dec 17 '16 at 23:34
  • Edit you question with the error (not in comments) and include the view. (clearly your view is not correct since its not sending the file in the request) –  Dec 17 '16 at 23:40
  • 2
    Because your file input is named `Event` (not `EventPhoto`) so its `HttpPostedFileBase poImgFile = Request.Files["Event"];` But this is awful practice. Use a view model which includes a property `public HttpPostedFileBase Photo { get; set; }` and in the view `@Html.TextBoxFor(m => m.Photo, new { type="file", accept="..." })` to bind to you model and post back that model. –  Dec 18 '16 at 00:13
  • Fixed it! Thank you! – ivanov.g94 Dec 18 '16 at 00:19
  • Do you know an easy way to fetch and display the stored photo that will work with my code? – ivanov.g94 Dec 18 '16 at 00:55
  • If you have another question, ask another question (and read the help files first) –  Dec 18 '16 at 01:20

0 Answers0