Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying images from database in an ASP.NET MVC View

I'm new to Asp.NET MVC development and a I want to make an application that displays multiple images from a database in the same View. I'm storing the images as LONGBLOB and converting then to a base 64 string.

I'm trying to display them like this:

<img src=<%:Html.Value("photo")%> />

However, I think that the URL doesn't support too big strings as parameters.

My Question is: Which is the best way to display images from database? I need to store them in a image server? Create a maps route to save the images in each URL?

I hope it was clear.

like image 622
Gabriel Langer Avatar asked Nov 24 '22 14:11

Gabriel Langer


1 Answers

You could try something like this:

  • Create a controller action that returns a FileContentResult
  • Pull the data from the database into either a Stream or a Byte array
  • From the action, use one of the Controller.File Method Overloads, passing in your image content and mime type, and return that from the action.
  • Set your img src attribute to the URL for the controller action, passing whatever parameters are needed to identify the image you want to return from the database.
like image 155
echo Avatar answered May 14 '23 04:05

echo