InfoPath 2007 File Attachment Control and Browser-Enabled Forms
This link describes the technical details on how this field is formatted.
Basically, there is a header which is of length 24 bytes + length of the filename. Following this head is the file contents. These are base64 encoded.
To work with the uploaded file in .NET code in the InfoPath form you can use code similar to the following (which shows a button handler that copies the uploaded file to a local directory):
public void Button_Clicked(object sender, ClickedEventArgs e)
XPathNavigator docXN = this.CreateNavigator();
XPathNavigator opnXN = docXN.SelectSingleNode
byte attachmentNodeBytes = Convert.FromBase64String
// 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 * 2;
byte fnBytes = new byte[fnLength];
// The actual filename starts at position 24 . . .
for (int i = 0; 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 = 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];
// We now have an array of bytes (fileContents) which we can
// do what we like with. I'll write them to a temporary file
FileStream fs = new FileStream(@"C:\Temp\InfoPath Test\" + fileName, FileMode.Create);
fs.Write(fileContents, 0, fileContents.Length); fs.Close();
Note that since this code is part of the InfoPath 2007 form the InfoPath form will need to have full trust (Tools -> Form Options -> Security and Trusts tab) and hence will need to be Admin deployed to the Microsoft Office SharePoint Server 2007 via Central Administration (don't forget to publish the form to a network location first !).
I'm going to try to incorporate this technique into an InfoPath form used in a custom developed workflow developed using the Sequential SharePoint Workflow template from the Enterprise Content Management Starter Kit, which can be downloaded here.