Posts

Showing posts from October, 2015

Storing and Retrieving Images in SQL Server

I typically use the File System for storing images, that doesn't work so well when using a web farm.  Below is an example of storing the image information into SQL server and then retrieving and rendering image back on the page. The datatype for the image column I am using is the varbinary(max).  For this example I am allowing the upload of an image so I limit both the size and file extension type. Upload Action [HttpPost] public ActionResult Upload(HttpPostedFileBase file, DataModel.MyClass imginfo) { if (ModelState.IsValid)    {      if (file == null)     {            } else if (file.ContentLength > 0)     { int MaxContentLength = 1024 * 1024 * 3; //3 MB - Could put this in a config string[] AllowedFileExtensions = new string[] { ".jpg", ".gif", ".png", ".JPG", ".jpeg", ".JPEG", ".GIF", ".PNG" }; if (!AllowedFileExtensions.Contains(file.FileName.Substring(file.FileName.La