2014-12-04 91 views
0

在我們的情況下,我們使用Microsoft Dynamics NAV將文件存儲在SQL數據庫中。之後,我們將此blob/image數據轉換爲base64字符串,通過SOAP服務將其傳輸並再次保存爲文件。生成的文件總是比原始文件大得多。文件大小似乎遵循2倍的獨特模式。120KB,248KB,504KB,1016KB,2040KB等。以SQL映像形式存儲並以64位字符串形式傳輸後的文件大小字符串

示例: 存儲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); 
} 
+0

此代碼[看起來不錯](http://msdn.microsoft.com/en-us/library/system.convert.frombase64string%28v=vs.110%29.aspx),所以問題可能在於存儲或檢索數據。 – CodeCaster 2014-12-04 11:45:46

+0

在調試時我看到一個模式。所有的字符串都填充了很多'A'。一些字符串最後有成千上萬的A.全部以A ==結尾。 – 2014-12-04 14:41:48

+0

在幾乎所有base 64實現中都給定了'A = 0',在某個地方你不會保存內容長度,而是實際的整個緩衝區。假設你使用的是1024字節的緩衝區,而你的內容只有1字節長,似乎是節省了1023個「0」。 – CodeCaster 2014-12-04 15:13:04

回答

1

任何有興趣; 我正在使用MemoryStream上的GetBuffer方法將文件數據轉換爲base64。原來,這也給你所有分配的內存空間,包括未使用的字節。使用ToArray方法僅返回數據本身。

相關問題