【发布时间】:2014-10-13 19:42:47
【问题描述】:
我有这个:
型号:
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"/>
和控制器:
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);
}
但我想将图像保存到数据库,而不仅仅是文件夹。但是如何在控制器动作中做到这一点?这些图像现在存储在地图中,而不是在数据库中
谢谢
这是编辑:
[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 但不在数据库中,列:Image - 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
【问题讨论】:
-
不一样的,如标题所说:将Image保存在物理路径中。但我想将其保存在数据库中而不是物理地图中
标签: c# database visual-studio-2012 entity-framework-5