Get file contents from attachment clip

Is there any way to get a file's contents from an attachment clip? Basically, I want to upload an XML folder with some data in it, open the file in code, perform validations on the file's contents, and throw a flag to kick off a new folder based on the validation result. I couldn't see any way to do this with the attachment clip object, but I feel like I must be missing something. Does anyone know if this is possible?

Tagged:

Comments

  • You should use ReadAttachment to fetch the contents of a clip into memory.

  • I tried this and it appears that the file contents are encrypted. How can I decrypt the file contents so I can read what is actually in the file?

  • If you have a binary file you can read it to a byte stream by using the GetAttachment function (writes the file to disk) and then use the .NET framework to read the file contents back to memory.  Don't forget to delete the file you have written out using GetAttachment.

     

    Hope this helps.

  • ReadAttachment will return the file as a base64 encoded string.

     

    If you wanted to, for example, convert this into an XMLDocument object you could do something like the following (Local.Attachment1 is a variable bound to a single clip field):

     

    string document = Mstm.ReadAttachment( ProcessContext.FolderId, Local.Attachment1 );
    byte[] docBytes = Convert.FromBase64String( document );
    
    using (System.IO.MemoryStream ms = new System.IO.MemoryStream(docBytes))
    {
        System.Xml.XmlDocument xd = new System.Xml.XmlDocument();   
        xd.Load(ms);
    
        //...
    }
    

     

    Hope that helps

     

    Iain

  • Hi Iain,

     

    Thanks for that, a much neater solution.  Maybe the Metastorm documentation could be improved to better indicate the type returned from Read Attachment.

     

    Thanks,

     

    Paul

  • Hi Paul

     

    I have raised a call with the helpdesk to get the documentation updated for this function so its clear that the returned string from ReadAttachment is base 64 encoded.

     

    Thanks

     

    Iain

  • Thanks Iain. I also raised a ticket with the help desk on this and got a more detailed response. Here is the code that they suggested I use:

     

    string document = Mstm.ReadAttachment( ProcessContext.FolderId, Local.Attachment1 );
    byte[] docBytes = Convert.FromBase64String( document );
    
    System.Text.Encoding encoding = new System.Text.ASCIIEncoding();
    string encodedFile = encoding.GetString(docBytes);
    Local.Memo1 = encodedFile;
    

     Local.Attachment1 is a local variable attached to a single-clip field. Once you get the data into docBytes, you can do anything you want with it. You can read it into an XML file, or apply encoding like I did above.

  • On a side note... I noticed there is no scripting developer's guide with Metastorm 9. I realize that most of it is just C# so there are lots of resources on the internet, but this might be useful to explain some of the internal Metastorm stuff.

  • While the scripting guide has been removed, I believe this information has been rolled into the Designer User Guide - section 31 has some good detailed information on server-scripting in v9.

  • Thanks Ari. I did see that there is a scripting section in the Designer User Guide. It's not as detailed as I remember the one in 7.6 being, but definitely useful. One thing that would be really helpful to have in the guide is some code snippets or examples of how to use some of the Metastorm objects, if possible. Thanks for pointing this out to me though.

  • Referencing back to Eric's Oct 18, '11 at 4:51 PM post, I tried the same approach trying to read a text file. Unfortunately, I cannot get it to properly read accented characters. They keep coming across as ? even though the original file, even as opened/saved from the clip, shows the text correctly.

    e.g. Telecomunicações comes across as Telecomunica??es using the approach above, or a box in those two positions when saving off the Mstm.GetAttachment() then Mstm.ReadFile().

    I need a solution quickly if anybody has suggestions. We are reading a tab delimited text file, generated from Excel.

    I have confirmed that Mstm.GetAttachment() which is stored on the OS is extracting and contains the correct text "Telecomunicações". So it now seems that the ReadFile is not interpreting them correctly, nor the bytestream conversion above.

    ç is ASCII 231; õ is ASCII 245. They seem to be converted to ASCII 63 with most approaches. I've even tried a System.IO.File.ReadAllLines(csFullPath) approach with the same end result.

  • Hi Brian, sorry for the delay,

    The example is using ascii encoding - although those chars mentioned appear in ASCII, they might be on a different page, or the source file might be unicode.

    You could try something replacing the ASCII Encoding with a different encoding like :
    byte[] AttachmentData = Convert.FromBase64String(Mstm.ReadAttachment(ProcessContext.FolderId, "SomeFile"));
    //DemoProcessData_Entity.MyMemo = System.Text.ASCIIEncoding.ASCII.GetString(AttachmentData);
    DemoProcessData_Entity.MyMemo = System.Text.UTF8Encoding.UTF8.GetString(AttachmentData);

    Hope this solves your problem (if you hadn't already found a solution)

    Rich