2011-12-17 28 views
2

我需要一个JPG图像保存到数据库 在这个属性的数据库类型是为nvarchar(最大) 所以我需要序列化和反序列化这属性是这样的:图像损坏或使用字节[]和varbinary(最大)时在NHibernate的截短

private byte[] HexStringToObject(string value) 
    { 
     SoapHexBinary shb = SoapHexBinary.Parse(value); 
     return shb.Value; 
    } 

private string ObjectToHexString(object value) 
    { 
     if (value == null) 
      return ""; 

     SoapHexBinary shb = new SoapHexBinary((byte[])value); 
     return shb.ToString(); 
    } 

,但有一个问题,,我不知道这个问题是序列化或反序列化方法 只是当我想通过将图像相关的:

public FileResult Initialpicture(int? propertyId) 
    { 
     if (propertyId == null) 
      return null; 
     IProperty image = Repository<IProperty>.Get(propertyId); 
     if (image.Value == null) 
      return null; 
     return File((byte[])image.Value, "image/jpg");//Get of Value use deserialize method to get byte[] 

和浏览器我只能看到图像的一小部分,并且出现错误“Image corrupt or truncated:” }

问题是什么?

+2

nvarchar(max)??你的意思是varbinary(max)? – 2011-12-17 06:36:16

+0

如果您使用Sql Server 2008或更高版本,则可以使用FILESTREAM。 – 2011-12-17 06:43:39

回答

2

也许您的数据库或ORM,如果您有任何,将例如字节数组截断到12000个字节。