使用byte []和varbinary(max)时,NHibernate中的图像损坏或被截断

时间:2011-12-17 06:33:29

标签: c# model-view-controller bytearray

我需要将jpg图像保存到数据库 在此属性的数据库类型中是nvarchar(max) 所以我需要像这样序列化和反序列化这个属性:

 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[]

并且在浏览器中我只能看到图像的一小部分,并且错误“图像损坏或截断:”         }

问题是什么?

1 个答案:

答案 0 :(得分:2)

如果您有数据库或ORM,可能会将字节数组截断为12000字节。

相关问题