简体   繁体   中英

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.

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.
public ActionResult MYsample()
{
    var MyList = storeDB.GenreList();
    var a= MyList.Count;

    if (a != null)
    {
        foreach (var li in MyList)
        {
            return File(li.fileContent, li.mimeType,li.fileName);
        }
    }
    return View(MyList);
}

View

@foreach (var item in Model)
{
        <img src="@Url.Action("MYsample", "HomeController", new { id = item.ProductID })" alt="@item.Productname" />
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM