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

C# MVC store image in database and return to view

mastersdevelopment
1#
mastersdevelopment Published in 2018-01-13 10:28:25Z

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.306036 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO