我有这个:
控制器:
[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);
path = Url.Content(Path.Combine("~/~/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);
}
观看:
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Motto)
</td>
<td>
@Html.DisplayFor(modelItem => item.PlaceOfBirth)
</td>
<td><img width="200px" height="150px" src="@Url.Content(item.Image)" /></td>
<td>
@Html.ActionLink("Details", "Details", new { id = item.Id })
</td>
</tr>
}
</table>
</div>
</div>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
所以我想在de index文件中显示图像,作为缩略图。图像已保存在数据库中,我尝试显示如下图像:
<td><img width="200px" height="150px" src="@Url.Content(item.Image)" /></td>
但这不起作用。
感谢您的帮助
我有索引,就像这样:
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Motto)
</td>
<td>
@Html.DisplayFor(modelItem => item.PlaceOfBirth)
</td>
<td>
<img width="200" height="150" src="@Url.Action("GetImage", "Account", new { item.Id })">
</td>
<td>
@Html.ActionLink("Details", "Details", new { id = item.Id })
</td>
</tr>
}
但我查看chrome并且我看到了尺寸:img 74x22并且也很奇怪,因为图像来自:
img {
background-image: url('../Images/Large.JPG');
background-repeat: no-repeat;
background-position: top;
background-size: cover;
width: 100%;
height: 100%;
因此,对于每个配置文件,图像都相同,但图像存储在:〜/ App_Data / uploads。
oke,我取消注释了site.css中的img {},现在我看到了正确的尺寸,但是图像被破坏了
GET http://localhost:41787/Account/GetImage/34 500 (Internal Server Error) Account:179
event.returnValue is deprecated. Please use the standard event.preventDefault() instead.
accountController现在看起来:
[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);
// path = Url.Content(Path.Combine("~/~/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.ImageMimeType = file.ContentType;
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);
}
public FileContentResult GetImage(int itemId)
{
UserProfile user = db.userProfiles.FirstOrDefault(u => u.Id.Equals(itemId));
if (user != null)
return File(user.Image, user.ImageMimeType);
else return null;
}
和视图(Index.cshtml):
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Motto)
</td>
<td>
@Html.DisplayFor(modelItem => item.PlaceOfBirth)
</td>
<td>
<img width="200" height="150" src='@Url.Action("GetImage", "Account", new { item.Id }, Request.Url.Scheme)'>
</td>
<td>
@Html.ActionLink("Details", "Details", new { id = item.Id })
</td>
</tr>
}
但这是正确的吗?
<td>
<img alt="" src="@Url.Action("GetImage", "Account", new {item.Id })" width="200" height="150" class="Image" />
</td>
答案 0 :(得分:0)
而不是
<img width="200px" height="150px" src="@Url.Content(item.Image)" />
把
<img width="200px" height="150px" src="@Url.Action("GetImage", "Account", new { item.Id })
在帐户控制器中添加此方法:
public FileContentResult GetImage(int itemId)
{
UserProfile user = db.userProfiles.FirstOrDefault(u => u.Id.Equals(itemId));
if(user != null)
return File(user.Image, user.ImageMimeType);
else return null;
}
您必须向用户表ImageMimeType添加新字段。
在本节中
// Update fields
user.Image = new byte[file.ContentLength];
file.InputStream.Read(user.Image, 0, file.ContentLength);
添加
user.ImageMimeType = file.ContentType;
就是这样。
希望它有所帮助。