gpt4 book ai didi

c# - 使用 asp.net mvc 4 + Entity Framework 将图像保存到数据库

转载 作者:太空狗 更新时间:2023-10-30 00:51:34 24 4
gpt4 key购买 nike

我有这个:

型号:

 public string Picture { get; set; }

[Column(TypeName = "image")]
public byte[] Image { get; set; }

[Display(Name = "Display profile Image")]
public bool DisplayItem { get; set; }

查看:

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

<input type="file" name="file"/>

和 Controller :

public ActionResult Edit(string UserId)
{
string username = User.Identity.Name;
// Fetch the userprofile
UserProfile user = db.userProfiles.FirstOrDefault(u => u.UserName.Equals(username));
// Construct the viewmodel

return View(user);
}

[HttpPost]
public ActionResult Edit(UserProfile userprofile, HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}

if (ModelState.IsValid)
{
string username = User.Identity.Name;
// Get the userprofile
UserProfile user = db.userProfiles.FirstOrDefault(u => u.UserName.Equals(username));

// Update fields

user.FirstName = userprofile.FirstName;
user.LastName = userprofile.LastName;
user.Email = userprofile.Email;
user.Motto = userprofile.Motto;

user.PlaceOfBirth = userprofile.PlaceOfBirth;
user.HowManyBikes = userprofile.HowManyBikes;
user.BesideYourBeth = userprofile.BesideYourBeth;
user.NicestRide = userprofile.NicestRide;
user.WorstRide = userprofile.WorstRide;
user.AmountKmPerYear = userprofile.AmountKmPerYear;
user.AverageSpeed = userprofile.AverageSpeed;
user.AbleToChatWhileRiding = userprofile.AbleToChatWhileRiding;
user.PhoneNumber = userprofile.PhoneNumber;

db.Entry(user).State = EntityState.Modified;

db.SaveChanges();

return RedirectToAction("Edit", "Account");
}

return View(userprofile);
}

但我想将图像保存到数据库,而不仅仅是文件夹。但是如何在 Controller Action 中做到这一点呢?图像现在存储在 map 中而不是数据库中

谢谢

这是编辑:

[HttpPost]
public ActionResult Edit(UserProfile userprofile, HttpPostedFileBase file)
{

if (file != null && file.ContentLength > 0)
{


// extract only the fielname
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}



if (ModelState.IsValid)
{
string username = User.Identity.Name;
// Get the userprofile
UserProfile user = db.userProfiles.FirstOrDefault(u => u.UserName.Equals(username));

// Update fields

user.FirstName = userprofile.FirstName;
user.LastName = userprofile.LastName;
user.Email = userprofile.Email;
user.Motto = userprofile.Motto;

user.PlaceOfBirth = userprofile.PlaceOfBirth;
user.HowManyBikes = userprofile.HowManyBikes;
user.BesideYourBeth = userprofile.BesideYourBeth;
user.NicestRide = userprofile.NicestRide;
user.WorstRide = userprofile.WorstRide;
user.AmountKmPerYear = userprofile.AmountKmPerYear;
user.AverageSpeed = userprofile.AverageSpeed;
user.AbleToChatWhileRiding = userprofile.AbleToChatWhileRiding;
user.PhoneNumber = userprofile.PhoneNumber;

db.Entry(user).State = EntityState.Modified;

db.SaveChanges();

return RedirectToAction("Edit", "Account");
}

return View(userprofile);
}

好的,我现在有它了:

[HttpPost]
public ActionResult Edit(UserProfile userprofile, HttpPostedFileBase file)
{

if (file != null && file.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/App_Data/uploads folder

userprofile.Image = new byte[file.ContentLength];
file.InputStream.Read(userprofile.Image, 0, file.ContentLength);

var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
etc...

我在文件夹中看到图像:~/App_Data/uploads 但不在数据库中,列:图像 - NULL

我现在是这样的:

 [HttpPost]
public ActionResult Edit(UserProfile userprofile, HttpPostedFileBase file)
{

if (file != null && file.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/App_Data/uploads folder



var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}



if (ModelState.IsValid)
{
string username = User.Identity.Name;
// Get the userprofile
UserProfile user = db.userProfiles.FirstOrDefault(u => u.UserName.Equals(username));

// Update fields

user.FirstName = userprofile.FirstName;
user.LastName = userprofile.LastName;
user.Email = userprofile.Email;
user.Motto = userprofile.Motto;

user.PlaceOfBirth = userprofile.PlaceOfBirth;
user.HowManyBikes = userprofile.HowManyBikes;
user.BesideYourBeth = userprofile.BesideYourBeth;
user.NicestRide = userprofile.NicestRide;
user.WorstRide = userprofile.WorstRide;
user.AmountKmPerYear = userprofile.AmountKmPerYear;
user.AverageSpeed = userprofile.AverageSpeed;
user.AbleToChatWhileRiding = userprofile.AbleToChatWhileRiding;
user.PhoneNumber = userprofile.PhoneNumber;

userprofile.Image = new byte[file.ContentLength];
file.InputStream.Read(userprofile.Image, 0, file.ContentLength);

db.Entry(user).State = EntityState.Modified;

db.SaveChanges();

return RedirectToAction("Edit", "Account");
}

return View(userprofile);
}

但仍然在数据库中 Image 为 NULL

最佳答案

我假设您需要保存在用户配置文件表中的图像。

您需要将此字段添加到用户实体:

public byte[] Image { get;set; }

并设置

user.Image = new byte[file.ContentLength];
file.InputStream.Read(user.Image,0, file.ContentLength);

希望这对您有所帮助。

完整示例在这里:

[HttpPost]
public ActionResult Edit(UserProfile userprofile, HttpPostedFileBase file)
{

if (file != null && file.ContentLength > 0)
{
// extract only the fieldname
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}

if (ModelState.IsValid)
{
string username = User.Identity.Name;
// Get the userprofile
UserProfile user = db.userProfiles.FirstOrDefault(u => u.UserName.Equals(username));

// Update fields
user.Image = new byte[file.ContentLength];
file.InputStream.Read(user.Image,0, file.ContentLength);

user.FirstName = userprofile.FirstName;
user.LastName = userprofile.LastName;
user.Email = userprofile.Email;
user.Motto = userprofile.Motto;

user.PlaceOfBirth = userprofile.PlaceOfBirth;
user.HowManyBikes = userprofile.HowManyBikes;
user.BesideYourBeth = userprofile.BesideYourBeth;
user.NicestRide = userprofile.NicestRide;
user.WorstRide = userprofile.WorstRide;
user.AmountKmPerYear = userprofile.AmountKmPerYear;
user.AverageSpeed = userprofile.AverageSpeed;
user.AbleToChatWhileRiding = userprofile.AbleToChatWhileRiding;
user.PhoneNumber = userprofile.PhoneNumber;

db.Entry(user).State = EntityState.Modified;

db.SaveChanges();

return RedirectToAction("Edit", "Account");
}

return View(userprofile);
}

关于c# - 使用 asp.net mvc 4 + Entity Framework 将图像保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26347705/

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