cancel
Showing results for 
Search instead for 
Did you mean: 

DotCMIS content update writing first byte only

lordzoster
Champ in-the-making
Champ in-the-making
Hallo
I'm trying to update a document content using DotCMIS and C#, but the content is getting the first byte only.
I followed an example I found over the net (here) and matching it with Apache CMIS documentation written in Java (here).
In any way I'm getting a file with the first character only, eg. "T" in the example string.
The only difference I see is the UTF-8 encoding: in my code, the string.toCharArray() method use Unicode encoding.
But at runtime the "file" variable has the correct length (36 bytes) and Alfresco reports the same length for the final document.
With a MIME type of "text/plain" the content has correct length but wrong charset.
Any advice will be greatly appreciated, thanks in advance!

             // fake file
            string fileContent = "This is content for a fake file";
            byte[] file = GetBytes(fileContent);

            // define new document stream object
            ContentStream contentStream = new ContentStream
            {
                FileName = fileName,
                Length = file.Length,             
                MimeType = "application/msword",
                Stream = new MemoryStream( file) 
            };

            Document doc = ((Document)session.GetObjectByPath(parentFolder.Path +"/" + fileName));
               
            doc.UpdateProperties(properties);
            doc.SetContentStream(contentStream, true);
where GetBytes is a custom defined function

static byte[] GetBytes(string str)
        {
            byte[] bytes = new byte[str.Length * sizeof(char)];
            System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
            return bytes;
        }
4 REPLIES 4

mrogers
Star Contributor
Star Contributor
Can't help you with th c# bits.  But
a) can you specify the content encoding anywhere?  In particular in the contentstream.   I'm wondering if the default will be some windows charset.

B) There's equivalent string to byte conversion methods in java that do not specify charset.   They are a menace and a frequent source of bugs and should be avoided ( for methods that do specify the charset)

C) the length of your variable bytes is dodgy.  UTF8 is 1 to 4 bytes per char.

lordzoster
Champ in-the-making
Champ in-the-making
Hallo thank you, the problem was actually encoding.
I solved using System.Text.Encoding.UTF8.GetBytes(string) that, along with "charset=UTF-8", gave the correct results.

mrogers
Star Contributor
Star Contributor
And your mimetype is wrong.  Should be "text/plain"

lordzoster
Champ in-the-making
Champ in-the-making
Why is "application/msword" wrong mimetype for creating a word binary file?
Actually the file now gets created correctly, and when viewed offline is OK, but Alfresco states it cannot preview the file content.
In my project I'm going to programmatically create a in-memory DOCX using OpenXML SDK and then upload it to Alfresco via CMIS.
How else could I upload/create (if not exists) a binary file like this?