我有一个包含输入字段和文件上传字段的表单,我认为我已经做了所有事情,但我对二进制阅读器有问题。 型号:
public class Event
{
[Key]
public int Id { get; set; }
public string EventName { get; set; }
public byte[] EventPhoto { get; set; }
}
控制器:
[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);
}
查看:
@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" />
}
发布错误:
>应用程序中的服务器错误。对象引用未设置为对象的实例。 NullReferenceException:未将对象引用设置为对象的实例。
第83行:使用(var binary = new&gt; BinaryReader(poImgFile.InputStream))