views:

485

answers:

1

This is somewhat related to another question i've asked but i figure why not ask it seperately.

If i were to place something like the following in aview

<td><img src='<%= Url.Action( "DisplayImage" , "User" , new { id = item.id} ) %>' alt="" /></td>

Is it supposed to display this

<td>
   <img src='/User.mvc/DisplayImage?id=U00915441' alt="" />
</td>

or would the src actually be replaced with the results of the UserController GetImage Action?

Thanks

+2  A: 

It will construct the path to the action, returning a url, not the results of executing the action.

The results will be:

<td>
   <img src='/User.mvc/DisplayImage?id=U00915441' alt="" />
</td>

Example code. assumes your user model has the image stored in a byte array. If you are using LINQ and the property is a Binary, then use the ToArray() method to convert it to a byte array. Note the attributes which will require that the user be logged in and using a GET request.

[Authorize]
[AcceptVerbs( HttpVerbs.Get )]
public ActionResult DisplayImage( string id )
{
     var user = ...get user from database...

     return File( user.Image, "image/jpeg" );
}

}

tvanfosson
Thanks. Would you happen know how images are supposed to be displayed using this method? Every example i've seen for displaying images uses this method and i don't understand how it would work (since it is not working for me). I'm new to mvc so this would really help.
zSysop
You would need a method called DisplayImage (or GetImage, you have it both ways but only one could be correct) that takes the user's id as a parameter. It would need to return a ContentResult with the appropriate mime type for the image format, say image/jpeg, image/gif, or image/png. Your action would retrieve the image from the database into a stream and construct the ContentResult from this stream, setting the mime type.
tvanfosson
Thanks tvan. Are there any examples on how to go about constructing a ContentResult for an image stored in a byte array?
zSysop
Now that I think about it you probably want a FileContentResult. Will update with some code.
tvanfosson