Which DataType should be used for Editor & File an

2019-08-10 09:56发布

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

  • Editor & File
  • Image browser

1条回答
唯我独甜
2楼-- · 2019-08-10 10:24

In my application I sotre image as base64string hence, nvarchar(max) is the data type i used. The advangage of this is that you can pass based64String image in JSON object from or to website.

To convert image to based64string

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 like this

image.ToBased64String

To convert based64String 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;
  }
}
查看更多
登录 后发表回答