2016-08-03 66 views
1

我能夠上傳文檔,但是當我查看/下載文檔時,似乎出現錯誤。它說它打開這個PDF時遇到了問題。 Ran into a problem從CRM上傳文檔正文到SharePoint註釋C#

我有以下代碼

using (var stream = new System.IO.MemoryStream()) 
{ 
    byte[] myByte = System.Text.ASCIIEncoding.Default.GetBytes(documentBody); 
    foreach (byte element in myByte) 
    { 
     stream.WriteByte(element); 
    } 
    stream.Seek(0, SeekOrigin.Begin); 
    var newFile = new FileCreationInformation { Url = fileName, ContentStream = stream, Overwrite = true }; 

    file = list.RootFolder.Files.Add(newFile); 
    file.CheckOut(); 
    file.CheckIn(string.Empty, CheckinType.MajorCheckIn); 
    context.Load(file); 
    context.ExecuteQuery(); 
} 

documentBody是場documentbodyAnnotation(注)。 stream有什麼問題嗎?

+0

所以你想通過從documentBody,這是一個簡單的字符串寫入字節來創建一個PDF?如果你想創建一個PDF,它需要遵循[這裏]描述的格式(http://www.adobe.com/content/dam/Adobe/en/technology/pdfs/PDF_Day_A_Look_Inside.pdf)。 – rene

+0

謝謝@rene,但我使用'documentbody'作爲字符串將SharePoint文檔複製到CRM Notes,我可以很好地查看它。它還適用,此外,我不是從頭創建一個PDF ... –

+0

除了低效率,這個代碼沒有太大的錯誤。什麼是文件名?在那個文檔裏,只有純文本?但你希望返回PDF? – rene

回答

1

documentBody是CRM中的Base64編碼,因此您需要在保存到SharePoint之前先解碼它。

試試這個來獲取文檔數據。

byte[] data = Convert.FromBase64String(e.Attributes["documentbody"].ToString());

+0

謝謝!有效!不過,我相信這個限制的長度是2,147,483,647。很好的通用文件不超過2GB,所以我認爲這足夠了。 –