Get Attachment Name - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Get Attachment Name

Last post 03-31-2023 10:50 AM by Rockie. 23 replies.
Page 1 of 2 (24 items) 1 2 Next >
Sort Posts: Previous Next
  • 04-12-2010 01:32 PM

    • Lawson
    • Top 100 Contributor
    • Joined on 12-24-2009
    • Virginia
    • Posts 91

    Get Attachment Name

    I have an IP 2003 form with C# code. The form has many attachment fields. I need to get the names of attachments and the document type from the attachment and place it in an output.

     

    Does anybody know how to get the file name and document type from an attachment?

    Example: document.doc

     Thanks,

    -Lawson

     

  • 04-14-2010 07:01 AM In reply to

    string attachedFile = MainDataSource.CreateNavigator().SelectSingleNode("attachment control XPATH", NamespaceManager).Value;

    Encoding _encoding = Encoding.Unicode;

    using (MemoryStream _memoryStream = new MemoryStream(Convert.FromBase64String(attachedFile)))

    {

    BinaryReader _theReader = new BinaryReader(_memoryStream);

    byte[] _headerData = _theReader.ReadBytes(16);

    int _fileSize = (int)_theReader.ReadUInt32();

    int _attachmentNameLength = (int)_theReader.ReadUInt32() * 2;

    byte[] _fileNameBytes = _theReader.ReadBytes(_attachmentNameLength);

    string fileName = _encoding.GetString(_fileNameBytes, 0, _attachmentNameLength - 2);

    }

    this works in infopath07, with some minor changes it should work in infopath 03 also

    Qazi Anis
    Technical Architect
    Bitwise Inc
  • 04-14-2010 09:09 AM In reply to

    • Lawson
    • Top 100 Contributor
    • Joined on 12-24-2009
    • Virginia
    • Posts 91

    Will the local variable "filename" give me the output I need?

  • 04-14-2010 09:20 AM In reply to

    • Lawson
    • Top 100 Contributor
    • Joined on 12-24-2009
    • Virginia
    • Posts 91

    I tried this, but I got over a dozen errors...

  • 04-14-2010 10:51 PM In reply to

    Yes fileName gives you the file name, you will need to add references. On my code i have below references

    using Microsoft.Office.InfoPath;

    using System;

    using System.Xml;

    using System.Xml.XPath;

    using System.Text;

    using System.Security.Cryptography;

    using System.Collections.Generic;

    using System.IO;

    using System.Windows.Forms;

    using mshtml;

    i dont know which all you need.

    The code works for me in IP07

    Qazi Anis
    Technical Architect
    Bitwise Inc
  • 04-15-2010 05:54 AM In reply to

    • Lawson
    • Top 100 Contributor
    • Joined on 12-24-2009
    • Virginia
    • Posts 91

    Can you try running this in IP 03 compatable mode to see if it will work with InfoPath 2003?

     Thanks for your help,

    -Lawson

  • 04-15-2010 06:18 AM In reply to

    string attachedFile = thisXDocument.DOM.selectSingleNode("/my:myFields/my:field1").text;

    Encoding _encoding = Encoding.Unicode;

    using (MemoryStream _memoryStream = new MemoryStream(Convert.FromBase64String(attachedFile)))

    {

    BinaryReader _theReader = new BinaryReader(_memoryStream);

    byte[] _headerData = _theReader.ReadBytes(16);

    int _fileSize = (int)_theReader.ReadUInt32(); int _attachmentNameLength = (int)_theReader.ReadUInt32() * 2;

    byte[] _fileNameBytes = _theReader.ReadBytes(_attachmentNameLength);

    string fileName = _encoding.GetString(_fileNameBytes, 0, _attachmentNameLength - 2);

     

    }

    Qazi Anis
    Technical Architect
    Bitwise Inc
  • 04-15-2010 03:58 PM In reply to

    • Lawson
    • Top 100 Contributor
    • Joined on 12-24-2009
    • Virginia
    • Posts 91

    Just ran the code--it worked! 

    Thanks for the help!

    -Lawson

  • 04-15-2010 10:37 PM In reply to

    the difference between the two codes is just the method of retrieving infopath field values in code

    Qazi Anis
    Technical Architect
    Bitwise Inc
  • 04-19-2010 04:15 PM In reply to

    • Lawson
    • Top 100 Contributor
    • Joined on 12-24-2009
    • Virginia
    • Posts 91

    Qazi,

    Thank you for your help in this post.

    I have one more question. The attachment field that I am getting the attachment name from is in a repeating section. The solution you gave me above works well. But it only gets the name of one document in the repeating section.

    How can I use the solution above to get all the names?

     Thanks,

    -Lawson

     

  • 04-20-2010 06:03 AM In reply to

    string attachedFile = string.Empty;for (int i = 1; i <= dml.length; i++)

    {

    attachedFile = thisXDocument.DOM.selectSingleNode(
    "/my:myFields/my:group1/my:group2["+i+"]/my:field4").text;

     

    Encoding _encoding = Encoding.Unicode;

    using (MemoryStream _memoryStream = new MemoryStream(Convert.FromBase64String(attachedFile)))

    {

    BinaryReader _theReader = new BinaryReader(_memoryStream);

    byte[] _headerData = _theReader.ReadBytes(16);

    int _fileSize = (int)_theReader.ReadUInt32(); int _attachmentNameLength = (int)_theReader.ReadUInt32() * 2;

    byte[] _fileNameBytes = _theReader.ReadBytes(_attachmentNameLength);

    string fileName = _encoding.GetString(_fileNameBytes, 0, _attachmentNameLength - 2);thisXDocument.DOM.selectSingleNode("/my:myFields/my:group1/my:group2[" + i + "]/my:field1").text = fileName;

    }

    }

    Qazi Anis
    Technical Architect
    Bitwise Inc
  • 05-03-2010 02:12 PM In reply to

    First, I am relatively new to the Infopath world and programing in general but I would like to say thank you for sharing this bit of code. Second, I have it working (mostly) but I get an error when it runs. It looks like it is trying to read past the EOF. What I am trying to do is fill in a read-only field on the form with the name of the attachment when the file is attached.

     Any help would be appreciated.

  • 05-03-2010 07:34 PM In reply to

     Are you trying to use this with image files?  I believe that images are not stored with the InfoPath header information as other file types are, and will cause erratic behavior in the above code.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 05-04-2010 09:38 AM In reply to

    I am trying to attach a variaty of file types (mainly pdf, jpeg, word, excel and powerpoint). And it appears that the error only happens when previewing from the design view so I am not sure how much of an issue it is. I am hoping that it does not cause issues further down the process.

    I am now trying to get the filename for an attachment control in a repeating table and running into issues. Will beg for help if I get completely stuck. (Or run out of time)

  • 05-04-2010 05:57 PM In reply to

     Hello I am facing kind of similar issue..my problem is that i have a browser enabled infopath 2007 form in that form there is a file attachment control in which users can upload documents nowi want to restrict the file types to be only of doc and pdf ...since selecting allowable file types is not there in browser based forms how do i do that...I got this code from a website but this does'nt work properly..it displays error but it displays for the PDF file also which it should allow..can somebody plz tell me what's wrong with the code...any help is really very much appreciated..

       public class InfopathAttachmentDecoder
            {
                private const int SP1Header_Size = 20;
                private const int FIXED_HEADER = 16;
                private int fileSize;
                private int attachmentNameLength;
                private string attachmentName;
                private byte[] decodeAttachment;

                public InfopathAttachmentDecoder(string theBase64EncodedString)
                {
                   byte[] theData = Convert.FromBase64String(theBase64EncodedString );
                    using(MemoryStream ms = new MemoryStream(theData ) )
                    {
                      BinaryReader theReader= new BinaryReader(ms);
                        DecodeAttachment(theReader );

                    }
                
                }

                private void DecodeAttachment(BinaryReader theReader)
                {
                    byte[] headerData = new byte[FIXED_HEADER];
                    headerData = theReader.ReadBytes(headerData.Length);
                    fileSize = (int)theReader.ReadUInt32();
                    attachmentNameLength = (int)theReader.ReadUInt32() * 2;
                    byte[] fileNameBytes = theReader.ReadBytes(attachmentNameLength);
                   // byte[] fileNameBytes = theReader.ReadBytes(attachmentNameLength);
                    Encoding enc = Encoding.Unicode;
                    attachmentName = enc.GetString(fileNameBytes, 0, attachmentNameLength  );
                    decodeAttachment = theReader.ReadBytes(fileSize);
                    

                  }

               

                public string Filename
                {
                    get { return attachmentName; }
                
                }
                public byte[] DecodedAttachment
                {
                    get { return decodeAttachment; }
                
                }

            }


            public void image_Validating(object sender, XmlValidatingEventArgs e)
            {
                 if(!e.UndoRedo && e.Operation == XmlOperation.ValueChange)
                {
                    string base64string = e.Site.Value;
                        
                    if(!string.IsNullOrEmpty(base64string))
                             {
                                    InfopathAttachmentDecoder decoder=
                                               new InfopathAttachmentDecoder(base64string);
                                   string filename = decoder.Filename;
                                     byte[] data= decoder.DecodedAttachment;
                                     
                                     string fileExtension = filename.Substring(filename.IndexOf(" . ") + 1);                                
                                     if(fileExtension.ToUpper()!= "PDF")
                                        e.ReportError(e.Site,false,"Only pdf are allowed");
                       
                             
                                    

                             }
                      }
            }
        }

Page 1 of 2 (24 items) 1 2 Next >
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.