spacer
cornerspacercorner
Reply
Advisor
Milosz
Posts: 44
Registered: 05-27-2010
0 Kudos

Read attachment from eAttachments table

Hi, I need to read attachment directly from eAttachments table in c# code into byte array, does anyone know how to do it? i tried to figure out how it is encoded but with no luck (there are some differences between v7.6 and v9)

Valued Contributor
Paul
Posts: 146
Registered: 05-19-2010
0 Kudos

Re: Read attachment from eAttachments table

My understanding is that it is an encoded base64 string

Employee
Tony Wheeler
Posts: 67
Registered: 05-02-2011
0 Kudos

Re: Read attachment from eAttachments table

[ Edited ]

Hi Milosz,

 

Try the following (this is not tested, but it was converted from a working vb.net attachment extractor):

 

                                //Get the byte array from the database... ie.. do a select eContents where eKey = 'whatever'
                                byte[] databytes = dt.Rows[0][0];
                                
                                // Create a file and write the byte data to a file.
                                System.IO.FileStream oFileStream = null;
                                oFileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create);
                                oFileStream.Write(databytes, 0, databytes.Length);
                                oFileStream.Close();

 

*editted with full code below*

 

 

 

using System.Data.OleDb;

 

byte[] databytes;

 

//Note these are TABS, not spaces

string eKey = "1 oa authproc.xml";

 

//Split eKey on Tab... we'll need the file and extension later

string[] k = eKey.Split(Convert.ToChar("\t"));

 

using (OleDbConnection connection = newOleDbConnection(@"Provider=SQLOLEDB;Data Source=ServerName\SQLInstance;Initial Catalog=MetastormDbName;User ID=UserName;Password=Password;")) {

System.Data.

DataSet result = new System.Data.DataSet();

 

using (OleDbCommandcommand = connection.CreateCommand()) {

command.CommandText =

"select eContents from eAttachment where eKey = '" + eKey + "'";

command.CommandType =

CommandType.Text;

connection.Open();

using (OleDbDataAdapter resultAdapter = newOleDbDataAdapter(command)) {

resultAdapter.Fill(result);

databytes = (

byte[])result.Tables[0].Rows[0][0];

}

connection.Close();

}

// Create a file and write the byte data to a file.

System.IO.

FileStream oFileStream = null;

oFileStream =

new System.IO.FileStream(Application.StartupPath + @"\" + k[2], System.IO.FileMode.Create);

oFileStream.Write(databytes, 0, databytes.Length);

oFileStream.Close();

}

Advisor
Milosz
Posts: 44
Registered: 05-27-2010
0 Kudos

Re: Read attachment from eAttachments table

i tried this solution before but it does not work, in v7.6 it was encoded in base64 but unfortunately same code does not work with v9

Employee
Tony Wheeler
Posts: 67
Registered: 05-02-2011
0 Kudos

Re: Read attachment from eAttachments table

Hi Milosz,

 

You are correct. The method I posted above only works for clear ascii files (my test case). Upon further investigation, it appears as though the engine is doing something to binary files that it is not doing to acii files (compressing or possibly encrypting to ensure non-privy eyes can't get at a clear byte array through the database).

 

We can't read those binary attachments directly from the database... we must go through the ECL.WS like the following:

 

(This works for a logged in user (SysAdmin in this case) who is privy to the folder 0900000000000000000000000000807. This folder has an attachment called Test.pdf associated with it.)

 

            try
            {

                ServiceSessionState eclServiceSession = new ServiceSessionState();
                ServiceClient eclWS = new ServiceClient();

                FormField[] loginData = new FormField[] {
                new TextField() { Name = "username", Value = "SysAdmin", Culture = CultureInfo.InvariantCulture.ToString() },
                new TextField() { Name = "password", Value = Common.PasswordEncrypt(""), Culture = CultureInfo.InvariantCulture.ToString() }
            };

                eclServiceSession = eclWS.Login("WEB;", 0, "", loginData);

                if (eclWS.IsLoggedIn(eclServiceSession))
                {

                    byte[] databytes = eclWS.GetAttachment(eclServiceSession, "Test.pdf", AttachmentOwnerType.Folder, "0900000000000000000000000000807");

                    System.IO.FileStream oFileStream = null;

                    oFileStream = new System.IO.FileStream(Application.StartupPath + @"\Test.pdf", System.IO.FileMode.Create);

                    oFileStream.Write(databytes, 0, databytes.Length);

                    oFileStream.Close();

                }
                else
                {
                    MessageBox.Show("Cannot log you in.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

 

 

 

line spacer line
spacerFollow Metastorm on:
spacer Twitter YouTube Blog iTunes LinkedIn Metastorm Community Central, MC2
spacer Copyright © 2011 OpenText Corporation. All Rights Reserved.spacer About Metastormspacer Privacyspacer Legalspacer Site Mapspacer RSSspacer Contact Us
Microsoft Gold Certified Partner
Powered by Windows Azure
line spacer line