2012-06-23 37 views
0

CRM将附件保存在AnnotationBase基本表中。将附件文件保存在Microsoft Dynamics CRM的计算机上

如何将DocumentBody实体中的文本转换回文件并将其保存到文件系统?

我已得到documentbody字段的值,然后尝试将其写入我的计算机中,但我的文件已损坏。

我使用这个代码:

String DocumentBody = Convert.ToBase64String(
     newUnicodeEncoding().GetBytes("UEsDBBQABgAIAAAAIQDQf9XuxAEAAE4HAAATAAgCW0NvbnRlbnRfVHlwZXNd  Lnh/abtPgp4eu7+W68C2dvLaWtho32sTajdkFmweGeKMQYTD5MrcDFf")); 

using (FileStream fs = new FileStream("c:\\1.docx", FileMode.Create, FileAccess.Write)) 
{ 
    byte[] bytes = Convert.FromBase64String(DocumentBody); 
    fs.Write(bytes, 0, bytes.Length); 
} 

GetBytes的字符串是一样annotationBase表documentbody场。

+0

不明白为什么人们已经下调的问题。这非常恰当。 –

回答

1

下面是一直对我有效的代码 - 我可以证实这一点对于我使用CRM 4 SDK中的CRM 4所获取的数据有效。 18个月前,我做了一个几乎完全一样的项目,在那里我们必须将CRM的所有笔记和电子邮件归档。

如果您仍然有问题see the original source of this code

public static void ExportFile(string fileName, string content) 
{ 
    byte[] fileContent = Convert.FromBase64String(content); 
    using (FileStream file = new FileStream(fileName, FileMode.Create)) 
    { 
     using (BinaryWriter writer = new BinaryWriter(file)) 
     { 
      writer.Write(fileContent,0,fileContent.Length); 
      writer.Close(); 
     } 

     file.Close(); 
    } 
} 
相关问题