我一直在研究一個系統,允許我們的用戶上傳圖片到數據庫中。上傳圖像到數據庫似乎工作正常,但我似乎無法取回圖像,並顯示在我的看法。我沒有收到任何錯誤消息,圖片根本沒有顯示。這是我的代碼到目前爲止。如何從MVC中的數據庫檢索圖像?
控制器 /創建方法
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(UserAccount userAccount, HttpPostedFileBase upload)
{
try
{
if (ModelState.IsValid)
{
if (upload != null && upload.ContentLength > 0)
{
var photo = new UserImage
{
FileName = System.IO.Path.GetFileName(upload.FileName),
FileType = FileType.VesselImage,
ContentType = upload.ContentType
};
using (var reader = new System.IO.BinaryReader(upload.InputStream))
{
photo.Content = reader.ReadBytes(upload.ContentLength);
}
userAccount.UserImages = new List<UserImage> { photo };
}
db.UserAccounts.Add(userAccount);
db.SaveChanges();
return RedirectToAction("Index");
}//if
}//try
catch (RetryLimitExceededException /* dex */)
{
//Log the error (uncomment dex variable name and add a line here to write a log.
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
}
return View(userAccount);
}
控制器 /詳細方法 斷點表明圖像被發現,在這個階段中選擇。
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
UserAccount userAccount = db.UserAccounts
.Include(s => s.UserImages)
.SingleOrDefault(s => s.userId == id);
if (userAccount == null)
{
return HttpNotFound();
}
return View(userAccount);
}
查看 /詳細
@model Multiple_Table_Post.Models.UserAccount
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>UserAccount</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.userName)
</dt>
<dd>
@Html.DisplayFor(model => model.userName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.userLocation)
</dt>
<dd>
@Html.DisplayFor(model => model.userLocation)
</dd>
@if (Model.UserImages.Any(f => f.FileType == FileType.VesselImage))
{
<dt>
Image
</dt>
<dd>
<img src="~/[email protected](f => f.FileType == FileType.VesselImage).FileId" alt="avatar" />
</dd>
}
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.userId }) |
@Html.ActionLink("Back to List", "Index")
</p>
我覺得這事不對我的條件:
@if (Model.UserImages.Any(f => f.FileType == FileType.VesselImage))
{
<dt>
Image
</dt>
<dd>
<img src="~/[email protected](f => f.FileType == FileType.VesselImage).FileId" alt="avatar" />
</dd>
}
如果我在視圖中這一點上打破我可以看到,該圖像被選中,但是註釋被輸出。我什麼都不返回。
我也包含了我的模型,希望能夠深入瞭解這個問題的底部。在這裏,他們是:
型號/UserAccount
namespace Multiple_Table_Post.Models
{
using System;
using System.Collections.Generic;
public partial class UserAccount
{
public int userId { get; set; }
public string userName { get; set; }
public string userLocation { get; set; }
public virtual ICollection<UserImage> UserImages { get; set; }
}
}
型號/UserImage 命名Multiple_Table_Post.Models使用系統 { ; using System.Collections.Generic;
public partial class UserImage
{
public int FileId { get; set; }
public string FileName { get; set; }
public string ContentType { get; set; }
public byte[] Content { get; set; }
public Nullable<int> PersonId { get; set; }
public FileType FileType { get; set; }
public virtual UserAccount UserAccount { get; set; }
}
}
型號 /文件類型
namespace Multiple_Table_Post.Models
{
public enum FileType
{
VesselImage = 1, Photo
}
}
感謝,如果我錯過了任何可能使這更容易,請讓我知道。
更新:在這裏,我已經包含了有關係統在命中條件並且可以看到數據時正在執行的操作的斷點信息。
我通常使用' jbutler483
你可以在這個'〜/ File?id = ...'調用後面顯示代碼嗎?也許這個方法返回不正確的'Content-Type'頭部? –