3

I'm trying to display the picture associated with a user in my database (the picture field's data type is image) on a page - unfortunately the code below fails to do that.

HTML

<img src="/User/Picture/1" />

Controller Action

public byte[] Picture(int id){
    UserRepository r = new UserRepository();
    return r.Single(id).logo.ToArray();
}
Jimbo
  • 22,379
  • 42
  • 117
  • 159

2 Answers2

4

PROBLEM SOLVED

Apologies, I didn't read up enough on this!

All that needed to be done was make the Controller Action return FileContentResult

public FileContentResult Picture(int id)
{
    UserRepository r = new UserRepository();   
    return new FileContentResult(r.Single(id).logo.ToArray(), "image/jpeg");
}
splattne
  • 102,760
  • 52
  • 202
  • 249
Jimbo
  • 22,379
  • 42
  • 117
  • 159
0

This question has a lot of useful answers. You probably need the FileContentResult-related answers.

In short, you need to return appropriate ActionResult, and not just array of bytes.

Community
  • 1
  • 1
queen3
  • 15,333
  • 8
  • 64
  • 119