【发布时间】: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