使用 byte[] 和 varbinary(max) 时,NHibernate 中的图像损坏或被截断
我需要将 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[]
在浏览器中我只能看到图像的一小部分,并且出现错误“图像损坏或截断:” }
问题是什么?
I need to save a jpg image to database
in the database type of this property is nvarchar(max)
so i need to serialize and deserialize this property like this:
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();
}
but there is a problem,, I dont know the problem is related to serialize or deserialize method
just when I want to see this image via:
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[]
and in the browser i just can see small part of image,and the error " Image corrupt or truncated:"
}
what is the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的数据库或 ORM(如果有的话)可能会将字节数组截断为 12000 字节。
Probably your database or your ORM if you have any, will truncate the byte array for example to 12000 bytes.