存储为 SQL 映像并传输为 base 64 字符串后的文件大小

本文关键字:字符串 文件大小 传输 SQL 映像 存储 base | 更新日期: 2023-09-27 18:33:15

在我们的例子中,我们使用Microsoft Dynamics NAV将文件存储在SQL数据库中。 之后,我们获取此blob/图像数据,将其转换为base64字符串,通过SOAP服务传输它,然后再次将其另存为文件。生成的文件始终比原始文件大得多。文件大小似乎遵循系数为 2 的独特模式。120KB、248KB、504KB、1016KB、2040KB 等。

例:按照此路由存储 23.669.715 字节的文件会导致 33.546.240 字节的文件。

用于保存文件的 C# 代码:

string fileData = string.Empty;
WebshopMgt webService = Helpers.WebServices.GetWebshopService();
try
{
    webService.GetDocumentData(navDocument.IntegrationID, ref fileData);
}
catch (Exception ex)
{
    Log.Error(String.Format("Error retrieving document data for NAV Document '{0}'.",     navDocument.IntegrationID), ex);
    return Status.SetStatus(navDocument.IntegrationID, syncId, SyncStatus.Failed, ex.Message);
}
try
{
    byte[] buffer = Convert.FromBase64String(fileData);
    FileStream fileStream = File.Create(filePath);
    fileStream.Write(buffer, 0, buffer.Length);
    fileStream.Close();
}
catch (Exception ex)
{
    Log.Error(String.Format("Error saving document data for NAV Document '{0}'.", navDocument.IntegrationID), ex);
    return Status.SetStatus(navDocument.IntegrationID, syncId, SyncStatus.Failed, ex.Message);
}

存储为 SQL 映像并传输为 base 64 字符串后的文件大小

对于任何感兴趣的人;我正在使用MemoryStream上的GetBuffer方法将文件数据转换为base64。事实证明,这也为您提供了所有分配的内存空间,包括未使用的字节。使用 ToArray 方法仅返回数据本身。