C# 在MVC中从数据库上载和显示多个图像
我正在使用ASP.NET MVC,我正在尝试将多个图像上载到数据库,然后在视图中显示它们。我相信上传过程是有效的,但是当我试图显示图像时,只有显示替代文本,并且在Chrome调试中找不到资源错误 这是我的图像在我的视图中的显示方式C# 在MVC中从数据库上载和显示多个图像,c#,asp.net-mvc,image-uploading,C#,Asp.net Mvc,Image Uploading,我正在使用ASP.NET MVC,我正在尝试将多个图像上载到数据库,然后在视图中显示它们。我相信上传过程是有效的,但是当我试图显示图像时,只有显示替代文本,并且在Chrome调试中找不到资源错误 这是我的图像在我的视图中的显示方式 @model IList<InspectionPhoto> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"
@model IList<InspectionPhoto>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Photo Management</h4>
</div>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="modal-body">
<div class="form-horizontal">
@if (Model.Count > 0)
{
<div class="row">
@for (var i = 0; i < Model.Count(); i++)
{
<div class="row">
<div class="col-md-6">
<img src="@Url.Action( "ShowPhoto", "Inspection", new { id = Model[i].PhotoID } )" alt="Image not available" />
</div>
//continues
“显示照片”方法位于“我的检查”控制器中,因为照片视图是一个模式局部视图,在“检查”视图中单击按钮时显示
任何帮助都将不胜感激。如果需要更多信息,请告诉我 您的
ShowPhoto
方法返回一个位图,您试图在其中写入的是html的img src
标记,它需要一个url字符串。您应该修改ShowPhoto函数,以返回与已上载图像关联的url ShowPhoto
返回Bitmap
,但应返回文件
,如下所示。如果您的图像保存正确,那么以下内容应该可以正常工作
public ActionResult ShowPhoto(int id)
{
InspectionPhoto image = db.InspectionPhotoes.Find(id);
byte[] result = image.ImageBytes;
return File(result, "image/png");
}
我认为你应该返回文件内容
public FileContentResult GetImage(int id)
{
InspectionPhoto image = db.InspectionPhotoes.Find(id);
byte[] byteArray = image.ImageBytes ;
return byteArray != null
? new FileContentResult(byteArray, "image/jpeg")
: null;
}
1) 您是否在控制器中设置了断点?2) 控制器的名称是检查
还是检查
?3) 在您的视图中,您需要一个URL,但您的操作方法返回一个位图。。。
public FileContentResult GetImage(int id)
{
InspectionPhoto image = db.InspectionPhotoes.Find(id);
byte[] byteArray = image.ImageBytes ;
return byteArray != null
? new FileContentResult(byteArray, "image/jpeg")
: null;
}