Which DataType should be used for Editor & File and image browser

我只是一个虾纸丫 提交于 2019-12-11 09:54:15

问题


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:


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;
  }
}


来源:https://stackoverflow.com/questions/30055752/which-datatype-should-be-used-for-editor-file-and-image-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!