我有一个包含用户 ID 和图像列的视图。
这是我尝试检索图像的方法,但我不断收到一个带有红色 x 的框,而不是实际图像。
View
<td><img src="<%= Url.Action( "DisplayImage" , "User" , new { id = item.id} ) %>" alt="" /></td>
控制器
public FileContentResult DisplayImage(string id)
{
byte[] image = repository.GetImage(id);
return File(image, "image/jpg");
}
我也尝试过返回 ActionResult,但这也不起作用。
存储库
public Byte[] GetImage(string id)
{
var image = db.GetImage(id).First<GetImageResult>();
if (image == null)
return null;
return image.UserImage;
}
Linq TO SQL 类
[Function(Name="dbo.GetImage")]
public ISingleResult<GetImageResult> GetImage([Parameter(DbType="VarChar(8)")] string id)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), id);
return ((ISingleResult<GetImageResult>)(result.ReturnValue));
}
public partial class GetImageResult
{
private System.Byte[] _userImage;
public GetImageResult()
{
}
[Column(Storage="_userImage", DbType="Image")]
public System.Byte[] UserImage
{
get
{
return this._userImage;
}
set
{
if ((this. _userImage!= value))
{
this. _userImage = value;
}
}
}
}
我一整天都在自杀,试图让它发挥作用,但它就是不起作用。
存储过程的返回类型是一个整数(至少当我查看参数时
SQL Server Management Studio 它说整数),但我现在无法重新定义,可以吗?
它实际上是在 UserController 中使用正确的参数点击 DisplayImage Action 并返回 File(imageByteArray, "image/jpg") 但只显示一个带有红色 x 的框。
任何帮助将不胜感激。
编辑:我尝试通过在操作结果中添加 Response.BinaryWrite(image Byte Array) 并直接点击 url 来进行调试http://localhost/User/DisplayImage?id=10101010并且该用户的图像显示在 mspaint 中。
edit2:我还查看了源代码,该图像标签的 html 如下所示。
<td>
<img src='/User.mvc/GetImage?id=U00915441' alt="" />
</td>
Thanks