Home C# MVC store image in database and return to view
Reply: 0

C# MVC store image in database and return to view

user1484 Published in June 20, 2018, 1:22 pm

Scope - I am uploading a file (image) to a sqldb and need to return this image to the view when I load the page.

The file is saved to the db as a byte[] with file name and content type, so i dont think the problem is here.

I have previously returned the file as a Base64 string which was fine but want to avoid this due to inflated dom size.

I have the following method to return the file but don't know how to insert it. I don't want to use an AJAX call from the view, I want the MVC approved solution to serve the content.

public FileContentResult getFile(byte[] fileBytes, string fileMime, string fileName) {
return File(fileBytes, fileMime, fileName);

public ActionResult Show(string id) {
  using (var db = new DbContext) {
      var a = getFile(db.File.Content, db.File.Mime, db.File.Name);
      var page = db.Pages.FirstOrDefault(x => x.id == id);
      PageViewModel page = new PageViewModel {
                        PageContent = page.PageContent,
                        PageImage = a

(I know the code here is sketchy as the db.file stuff wont work but its just an example so you know what I mean.)

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.98044 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO