I have a view which contains a users id and an image column.
Here's what i've tried doing to retrieve the image but i keep getting a box with an red x instead of the actual image.
View
<td><img src="<%= Url.Action( "DisplayImage" , "User" , new { id = item.id} ) %>" alt="" /></td>
Controller
public FileContentResult DisplayImage(string id)
{
byte[] image = repository.GetImage(id);
return File(image, "image/jpg");
}
i've also tried returning an ActionResult instead and that didn't work either.
Repository
public Byte[] GetImage(string id)
{
var image = db.GetImage(id).First<GetImageResult>();
if (image == null)
return null;
return image.UserImage;
}
LinqTOSQL Class
[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;
}
}
}
}
I've been killing myself all day trying to get this to work, but it just isn't working. The return type on the stored procedure is an integer (atleast when i look at parameters in SQL Server Management Studio it says integer), but i can't redefine that now can i?
It's actually hitting the DisplayImage Action with the correct parameters within the UserController and returning File(imageByteArray, "image/jpg") but only a box with red x is being displayed. Any help would be greatly appreciated.
edit: I've tried debugging by adding a Reponse.BinaryWrite(imageByteArray) within the action result and hitting the url directly by goign to http://localhost/User/DisplayImage?id=10101010 and the image for that user is displayed in mspaint.
edit2: I also did a view source and my html for that image tag came out as following.
<td>
<img src='/User.mvc/GetImage?id=U00915441' alt="" />
</td>
Thanks