我有一个带有Entity Framework应用程序的MVC 5,数据库优先,我是MVC的新手。我有一个选择图像的文件选择器控件,在创建视图的Action Result类中,我编码图片以便在SQL中存储。我的编码是正确的,但是视图中的数据是在Bind属性中,我想在将其写入数据库之前使用编码图片更新Bind属性。当我尝试写入数据库时,整个应用程序只是简单地搅拌直到内存不足。显然,我错过了一块。
查看:这是带有filepicker的div,以及实体框架想要编码的图片去的地方。
<div class="col-md-10"><br/><br/>
@Html.TextBox("beforeEncode", "", new { type = "file" })<br />
@Html.LabelFor(model => model.fldPhoto, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.fldPhoto, new { htmlAttributes = new { @class = "form-control"} })
@Html.ValidationMessageFor(model => model.fldPhoto, "", new { @class = "text-danger" })
</div>
</div>
控制器:这是删除了不相关部分的控制器。我在db.SaveChanges()上设置了断点;用于检查编码是否存在问题的行,但即使将其删除,也只会产生
public ActionResult Create([Bind(Include = "fldPhotoID,fldPhoto,beforeEncode")] tblPhoto tblPhoto, String beforeEncode)
{
var fileupload = beforeEncode;
try
{
if (ModelState.IsValid)
{
if (fileupload != null)
{
FileStream fs;
fs = new FileStream(beforeEncode, FileMode.Open, FileAccess.Read);
byte[] PicEncoded = new byte[fs.Length];
fs.Read(PicEncoded, 0, Convert.ToInt32(fs.Length));
fs.Close();
tblPhoto.fldPhoto = PicEncoded;
}
db.tblPhotoes.Add(tblPhoto);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}