What type of data should be used for the editor and file and image browser

What data type should be used in the MSSQL database with the MVC application for the following fields, as on the following components? http://demos.telerik.com/aspnet-mvc/editor/index

  • Editor and file
  • Image browser
+1
source share
1 answer

In my application, sotre image is like base64string, therefore nvarchar (max) is the data type that I used. This means that you can transfer a base64String image in a JSON object from or to a website.

To convert an image to base64string

public static string ToBased64String(this Image image, ImageFormat format)
{
  using (MemoryStream ms = new MemoryStream())
  {
    image.Save(ms, format);
    byte[] imageBytes = ms.ToArray();
    string based64String = Convert.ToBased64String(imageBytes);
    return based64String;

  }
}

then you can call your method as follows

image.ToBased64String

Convert base64String to image

public static ImageFromBased64String(string based64Image, string path)
{
  Image image = null;
  var bytes = Convert.FromBased64String(based64String);
  using (var fileStream = new FileStream(path, FileMode.Create))
  {
    fileStream.Write(bytes, 0, bytes.Length);
    fileStream.Flush();
    image = Image.FromStream(fileStream, true);
    return image;
  }
}
0

All Articles