【问题标题】:Docx to byte array not saving in databaseDocx到字节数组未保存在数据库中
【发布时间】:2020-09-23 15:36:15
【问题描述】:

我正在尝试将.docx 文件保存在数据库中,此处显示的代码是将.docx 文件转换为字节数组,然后尝试将其保存到数据库中。

我遇到了一个错误

字符串或二进制数据将被截断

我在数据库中使用了 varbinary(max) 类型的列,相同的代码适用于 pdf 和文本文件,但不适用于 .docx

请指导我。

控制器:

try
{
    byte[] byteDocument = new byte[0];

    if (file.Length > 0)
    {
        long length = file.Length;

        using var fileStream = file.OpenReadStream();
        byteDocument = new byte[length];
        fileStream.Read(byteDocument, 0, (int)file.Length);

        _attachmentDto = new ReviewAttachmentDto
                    {
                        ReviewId = reviewId,                       
                        DocumentType = file.ContentType,
                        Document = byteDocument
                    };
    }

    string requestBody = JsonConvert.SerializeObject(_attachmentDto);

    // Call API
    var _responseObj = await WebAPIHelper.PostDataToAPI(appSettings.SaveUrl, requestBody;
}

数据库保存:

public void SaveAction(ReviewAttachment reviewAttachment)
{
    Entities.Surveillance.ReviewAttachment reviewAttachmentDB = new Entities.Surveillance.ReviewAttachment();
    reviewAttachmentDB.ReviewId = Int32.Parse(reviewAttachment.ReviewId);
    reviewAttachmentDB.DocumentType = reviewAttachment.DocumentType;
    reviewAttachmentDB.Document = reviewAttachment.Document;

    context.Add(reviewAttachmentDB);
    context.SaveChanges();
}

【问题讨论】:

    标签: c# .net asp.net-mvc asp.net-core


    【解决方案1】:

    由于我怀疑您的 Word 文档大小超过 2 GB,所以我建议您检查一下您是否没有达到实际数据库文件的大小限制。您可能需要在数据库上启用自动增长。在这里检查答案。此人在 varbinary(max) 列中收到相同的错误消息。

    https://stackoverflow.com/a/11006473/1461269

    此外,要了解启用此选项的含义,您可以在 Microsoft 的支持网站上阅读:https://support.microsoft.com/en-ca/help/315512/considerations-for-the-autogrow-and-autoshrink-settings-in-sql-server

    【讨论】:

    • 媒体类型长度为 127 但我的表长度为 100 varchar 导致此问题
    猜你喜欢
    • 2018-01-16
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    相关资源
    最近更新 更多