I previously wrote about how you can attach an entire InfoPath form to a SharePoint list item when you submit an InfoPath browser form to a SharePoint list.
As you may know, you can use a File Attachment control to attach a document to an InfoPath form.
If you want to save a file, which you have stored in a File Attachment control on an InfoPath form, as an attachment to a SharePoint list item, you can use the same technique I discussed in my previous blog post.
To do this, you must first decode the file attachment in the InfoPath form, that is, convert the base64 encoded string of the attachment in the InfoPath form into a byte array, by using the InfoPathAttachmentDecoder class discussed in How to encode and decode a file attachment programmatically by using Visual C# in InfoPath.
Then you can use the following code to save the byte array of the file as an attachment to a SharePoint list item:
// Retrieve the base64 encoded string of the attached file
string attachedFile =
MainDataSource.CreateNavigator().SelectSingleNode(
"//my:document", NamespaceManager).Value;
// Convert the base64 encoded string into a byte array
InfoPathAttachmentDecoder decoder =
new InfoPathAttachmentDecoder(attachedFile);
byte[] attachment = decoder.DecodedAttachment;
string fileName = decoder.Filename;
// Add the file as an attachment to the SharePoint list item
item.Attachments.Add(fileName, attachment);
where my:document is the XML node in the InfoPath form that represents a File Attachment control.
You must place this code before the item.Update() line.

Comments to this post were closed 30 days after it was published.