gpt4 book ai didi

asp.net-mvc-4 - 如何使用 MVC 4 显示数据库中的二进制图像以编辑表单

转载 作者:行者123 更新时间:2023-12-03 02:11:21 30 4
gpt4 key购买 nike

我已成功将图像作为二进制图像存储在数据库中。我想在编辑表单中显示这些图像以修改并保存更改。显示数据库中的二进制图像时,以 Edit.cshtml 形式抛出 System.OutOfMemoryException'。

有人可以更正我的代码吗?

模型类:

public class Accommodation
{
[Key]
public string A_Unique_Id { get; set; }

public byte[] Picture1 { get; set; }

public byte[] Picture2 { get; set; }

public byte[] Picture3 { get; set; }


}

//获取:/住宿/编辑/5

public ActionResult Edit(string id)
{
Accommodation accommodation = db.Accommodation.Find(id);
ViewBag.SelectedAustraliaStateId = new SelectList(db.AustraliaStates, "AustraliaStateId", "AustraliaStateName", accommodation.SelectedAustraliaStateId);

return View(accommodation);
}

//POST:/住宿/编辑/5

[HttpPost]
public ActionResult Edit(Accommodation accommodation)
{
if (ModelState.IsValid)
{
db.Entry(accommodation).State = EntityState.Modified;

//the following line is for re-assigning back the DDL modified value.
accommodation.State = accommodation.SelectedAustraliaStateId;

db.SaveChanges();
return RedirectToAction("Index");
}
return View(accommodation);
}

编辑.cshtml

<div class="editor-label">
@Html.LabelFor(model => model.Picture1)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Picture1)
@Html.ValidationMessageFor(model => model.Picture1)
</div>

错误消息:@Html.EditorFor(model => model.Picture1) 行抛出“System.OutOfMemoryException”类型的异常

最佳答案

在 View 中显示图像

查看

<form  method="post" enctype="multipart/form-data">
@{
if (Model.Picture1 != null)
{
string imageBase64 = Convert.ToBase64String(Model.Picture1);
string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);
<img src="@imageSrc" width="100" height="100" />
}
}
<input type="file" name="photo" id="files" accept="image/*;capture=camera">
<button type="button">Submit</button>
</form>

Controller

[HttpPost]
public ActionResult Edit(Accommodation accommodation)
{

if (Request.Files["files"] != null)
{
byte[] Image;
using (var binaryReader = new BinaryReader(Request.Files["files"].InputStream))
{
Image = binaryReader.ReadBytes(Request.Files["files"].ContentLength);
}
}
accommodation.Picture1=Image;
//your code to save data
}

关于asp.net-mvc-4 - 如何使用 MVC 4 显示数据库中的二进制图像以编辑表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21421535/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com