InfoPath File Attachment Control

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.