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

发布于 2024-12-21 11:04:29 字数 944 浏览 3 评论 0原文

我需要将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罗罗贝儿 2024-12-28 11:04:29

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

Probably your database or your ORM if you have any, will truncate the byte array for example to 12000 bytes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文