Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying a image from a database in asp.net mvc
    primarykey
    data
    text
    <p>I have a view which contains a users id and an image column.</p> <p>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.</p> <p>View</p> <pre><code>&lt;td&gt;&lt;img src="&lt;%= Url.Action( "DisplayImage" , "User" , new { id = item.id} ) %&gt;" alt="" /&gt;&lt;/td&gt; </code></pre> <p>Controller</p> <pre><code> public FileContentResult DisplayImage(string id) { byte[] image = repository.GetImage(id); return File(image, "image/jpg"); } </code></pre> <p>i've also tried returning an ActionResult instead and that didn't work either.</p> <p>Repository</p> <pre><code> public Byte[] GetImage(string id) { var image = db.GetImage(id).First&lt;GetImageResult&gt;(); if (image == null) return null; return image.UserImage; } </code></pre> <p>LinqTOSQL Class</p> <pre><code> [Function(Name="dbo.GetImage")] public ISingleResult&lt;GetImageResult&gt; GetImage([Parameter(DbType="VarChar(8)")] string id) { IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), id); return ((ISingleResult&lt;GetImageResult&gt;)(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; } } } } </code></pre> <p>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?</p> <p>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.</p> <p>edit: I've tried debugging by adding a Reponse.BinaryWrite(imageByteArray) within the action result and hitting the url directly by goign to <a href="http://localhost/User/DisplayImage?id=10101010" rel="nofollow noreferrer">http://localhost/User/DisplayImage?id=10101010</a> and the image for that user is displayed in mspaint.</p> <p>edit2: I also did a view source and my html for that image tag came out as following.</p> <pre><code>&lt;td&gt; &lt;img src='/User.mvc/GetImage?id=U00915441' alt="" /&gt; &lt;/td&gt; </code></pre> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload