I have this form where I'm trying to upload an image. WhenI click the submit button, I get the following error:
"The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters."
The controller is never reached, as the error happens as soon as I upload the image. I'm at a loss what to do. Any help is appreciated!
@using (Html.BeginForm("Create", "Create", FormMethod.Post, new { enctype = "multipart/form-data"}))
{
@Html.AntiForgeryToken()
<div class="form-group">
<div class="col-md-10">
@Html.LabelFor(model => model.ComponentModel.Image, htmlAttributes: new {@class = "control-label col-md-2"})
<a class="btn" href="javascript:;">
Choose File...
<input type="file" name="Image" Size="40" style="position: absolute; z-index: 2; top: 0; left: 0; filter: alpha(opacity=0); opacity: 0; background-color: transparent; color: transparent"
onchange='$("#upload-file-info").html($(this).val());'/>
</a>
<span class="label label-info" id="upload-file-info"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default"/>
</div>
</div>
}
UPDATE:
This is the Create controller:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Component component, HttpPostedFileBase image = null)
{
if (ModelState.IsValid)
{
if (image != null)
{
component.Image = new byte[image.ContentLength];
image.InputStream.Read(component.Image, 0, image.ContentLength);
}
componentRepository.InsertComponent(component);
componentRepository.Save();
return RedirectToAction("Index", "Home");
}
return RedirectToAction("Index", component);
}