SoftwareNerd
SoftwareNerd

Reputation: 1895

How to show image from a DB in WebAPI?

Hi all,

I have a table named Merchants in my database. I have to display the images using a WebAPI. I have seen tutorials but no tutorial have how to retrieve data from the database in WebAPI and how to display them. This is the first time I am working with WebAPI.

Can any one help me with the procedure to follow in WebAPI?

   ID   Image                                              StoreID   Status
   1    C:\Users\Administrator\Desktop\Images\k3673723.jpg  1         new
   2    C:\Users\Administrator\Desktop\Images\k7649737.jpg  2         new
  11    C:\Users\Administrator\Desktop\Images\Wallmart.jpg  4         new 

Upvotes: 0

Views: 1390

Answers (1)

Nick
Nick

Reputation: 6588

I don't know why you specifically need to use a WebApi Controller to achieve this? A regular MVC controller should suffice:

public class ImageController : Controller
{
    public FileResult Get(int id)
    {
        var file = "C:\\myimage.png";

        var bytes = System.IO.File.ReadAllBytes(file);

        return new FileContentResult(bytes, "image/png");
    }
}

Obviously you would replace the harcoded file path with your lookup to the database to get the image filepath for the Id provided.

You can then reach the image by hitting http://localhost/image/get/123

Upvotes: 1

Related Questions