Status: Open
Status: Answered
Status: Closed
Status: Duplicate

Get file contents from attachment clip

0
Posted Oct 03 by Eric Schiavo.

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?

12 Answers

0
BEST ANSWER: As chosen by the author.

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


0
BEST ANSWER: As chosen by the author.

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?


0
BEST ANSWER: As chosen by the author.

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.


0
BEST ANSWER: As chosen by the author.

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


0
BEST ANSWER: As chosen by the author.

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


0
BEST ANSWER: As chosen by the author.

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


1
BEST ANSWER: As chosen by the author.

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.


0
BEST ANSWER: As chosen by the author.

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.


0
BEST ANSWER: As chosen by the author.

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.


0
BEST ANSWER: As chosen by the author.

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.


0
BEST ANSWER: As chosen by the author.

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.


0
BEST ANSWER: As chosen by the author.

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


 You have subscribed and will receive email notifications of updates to this topic. To unsubscribe, uncheck the checkbox.

Statistics

Related categories

Your answer

To leave an answer, please sign in.