Microsoft InfoPath 2010
The official blog of the Microsoft InfoPath team

InfoPath File Attachment Control

InfoPath File Attachment Control

  • Comments 21

File Attachment Control

Applies to: Microsoft Office InfoPath 2003 SP1

Microsoft Office InfoPath 2003 SP1 introduces a number of new controls.  One of these new controls, the file attachment control, enables you to insert files which will be encoded and saved with the form. This control opens up a number of new scenarios not easily possible before SP1. 

For example, let’s say that you have created a form that will be used by your sales people in the field. Typically, your sales people may track large amount of sales data in an Excel spreadsheet which includes fancy charts and pivot tables.  When these sales people report their quarterly earnings, you would like them to fill out an InfoPath form so that you can gather data such as total sales per region.  However, since the sales people have already gathered all the detailed sales data in an Excel spreadsheet, you don’t want them to have to reenter the data into the InfoPath form.  Instead, your sales people click on a file attachment control and attach the spreadsheet containing their sales data.  Later, when you are reviewing the data in the InfoPath form, you can double-click the spreadsheet file and open it in Excel in order to view the detailed sales data.

Adding a file attachment control to a form is easy.  Just follow these four basic steps:

  1. Start InfoPath and design a new blank form or design an existing form.
  2. Open the Controls task pane by clicking on the Controls link in the Design Tasks pane.
  3. Look for the File Attachment control under the File and Picture category of the Controls task pane.
  4. Insert the file attachment control by clicking on it or dragging and dropping it into the view.

Now, when you preview the form or fill out a form based on this form template, users can attach files to your form.  Also, if you open a form that has existing file attachments, you can double-click on the control to open it.  When opening files that are already attached, by default, the application that is associated with that file’s extension on your system is opened in order to view the file.

Security

For security reasons, not all types of files can be attached to a form.  The last thing you want is for somebody to attach a malicious executable file that formats your hard drive.  For this reason, InfoPath blocks certain file types from being attached (e.g., .exe, .com, .bat, etc.).  To determine the types of files that are considered unsafe, InfoPath follows a similar model as Outlook which is documented in http://support.microsoft.com/default.aspx?scid=kb;EN-US;290497.

In addition to the list of file types blocked by default, you can further restrict the types of files you want users to attach to your form.  To do so, perform the following:

  1. Follow the steps above to insert a file attachment control into the view.
  2. Double-click on the file attachment control that you just inserted into your form in order to open the properties dialog for the control.
  3. On the Data tab of the properties dialog, check “Allow the user to attach only the following file types” option.
  4. In the textbox below this option, enter the extension or extensions of the file types that you want your users to be allowed to attach to the form.  Use semi-colons to separate multiple file extensions (e.g., doc; ppt; xls).

Technical Details

Upon attaching a file, InfoPath first builds a header structure which includes information about the file being attached. The structure consists of the following:

·         BYTE[4]: Signature (based on the signature for PNG):

(decimal)                       199       73         70         65
(hexadecimal)                C7        49         46         41 
(ASCII C notation)          \307      I           F          A 

The first byte is chosen as a non-ASCII value to reduce the probability that a text file may be misrecognized as a file attachment.  The rest identifies the file as an InfoPath File Attachment. 

·         DWORD: Size of the header

·         DWORD: IP Version

·         DWORD: dwReserved

·         DWORD: File size

·         DWORD: Size of file name buffer

·         File name buffer: variable size

After building the header structure described above, the header and file data are concatenated and base64 encoded.  This base64 data is then stored with the XML data in the form.  (More information about base64 encoding can be found here: http://www.w3.org/Protocols/rfc1341/5_Content-Transfer-Encoding.html.) 

If you wish to write code to work with InfoPath file attachments, the .NET framework provides a Convert class that includes methods to encode and decode base64 data (e.g., Convert.FromBase64String). These methods can be used to decode existing InfoPath file attachment data or to encode a file for storing in an InfoPath form.

When a file attachment control is inserted into an InfoPath form, the following processing instruction is inserted into the XML template for the form:

<?mso-infoPath-file-attachment-present?>

This processing-instruction is added as a convenience for InfoPath but is not meant to be a security feature. If this processing-instruction is removed, when users fill out the form, all file attachment controls will be disabled.

Leave a Comment
  • Please add 2 and 1 and type the answer here:
  • Post
  • I would like to Know if the sharepoint search indexes the content of the attached file.

    So i wanna know if i can find it if a search its contents.

  • Above, "AngieD" stated: I'm using Sharepoint 07 and Infopath 07.  When I tried to publish my form to Sharepoint including an attachment control, I get this message: "the selected field cannot be promoted because its data type is not supported: base 64binary".  When looking at changing the data type and viewing field/control properties, it doesn't appear that there are any other options for this type of field (attachment).  Is this because I am publishing a web browser form as opposed to Infopath? Any suggestions for making an attachment work with my form? "

    I have deployed the custom workflow action described here: http://www.deviantpoint.com/post/2009/06/06/Custom-SharePoint-Designer-workflow-for-extracting-InfoPath-attachment-fields.aspx

    It appears that this is possible if the person who create this workflow action can be believed... how was he able to make this work?

  • I also would like to know if the SharePoint search can search the contents of an InfoPath file attachment.  If not, will this be fixed in SP 2010?

  • XPathNavigator docXN = this.CreateNavigator();

               XPathNavigator opnXN = docXN.SelectSingleNode("/my:myFields/my:document", this.NamespaceManager);

               byte[] attachmentNodeBytes = Convert.FromBase64String(opnXN.ToString());

               // Position 20 contains a DWORD indicating the length of the

               // filename buffer. The filename is stored as Unicode so the

               // length is multiplied by 2.

               int fnLength = attachmentNodeBytes[20] * 2;

               byte[] fnBytes = new byte[fnLength];

               // The actual filename starts at position 24 . . .

               for (int i = 0; i < fnBytes.Length; i++)

               {

                   fnBytes[i] = attachmentNodeBytes[24 + i];

               }

               // Convert the filename bytes to a string. The string

               // terminates with \0 so the actual filename is the

               // original filename minus the last character !

               char[] charFileName = System.Text.UnicodeEncoding.Unicode.GetChars(fnBytes);

               string fileName = new string(charFileName);

               fileName = fileName.Substring(0, fileName.Length - 1);

               // The file is located after the header, which is 24 bytes long

               // plus the length of the filename.

               byte[] fileContents = new byte[attachmentNodeBytes.Length - (24 + fnLength)];

               for (int i = 0; i < fileContents.Length; ++i)

               {

                   fileContents[i] = attachmentNodeBytes[24 + fnLength + i];

               }

               string SiteURL = "http://LIBNAME/" + fileName;

               SPWeb site = new SPSite(SiteURL).OpenWeb();

               SPList list = site.Lists["Office Documets"];

               SPListItemCollection colListItems = list.GetItems();

               int count = 0;

               foreach (SPListItem item in colListItems)

               {

                  site.Files.Add(SiteURL, fileContents);

               }

           }

  • Is there a way to make the attachment field ready only or disable it for a timeframe?

  • Hi Jessica,

    Yes... just place it inside of a "section" control then you can add conditional formatting to the section.

    Scott

Page 2 of 2 (21 items) 12