Extracting Attachment from InfoPath form using C#

The sample code for extracting attachment from submitted InfoPath form

var lstTimeSheet = new List<RelatedTimesheet>();
 foreach (var timeSheet in invoiceFormData.Summary.Timesheet)
 {
 if (timeSheet.Attachment != null)
 {
 var tSheet = new RelatedTimesheet();
 var b = timeSheet.Attachment;
 var nameBufferLen = b[20] * 2;
 var fileNameBufffer = new byte[nameBufferLen];
 for (var i = 0; i < nameBufferLen; i++)
 {
 fileNameBufffer[i] = b[24 + i];
 }

 var charFileName = Encoding.Unicode.GetChars(fileNameBufffer);
 var fileName = new string(charFileName);
 tSheet.FileName = fileName.Substring(0, fileName.Length - 1); 

 var fileContent = new byte[b.Length - (24 + nameBufferLen)];
 for (var i = 0; i < fileContent.Length; i++)
 {
 fileContent[i] = b[24 + nameBufferLen + i];
 }

 tSheet.Attachment = fileContent;
 lstTimeSheet.Add(tSheet);
 }
 }

public class RelatedTimesheet
{
 public string FileName { get; set; }
 public byte[] Attachment { get; set; }
}

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

1 thought on “Extracting Attachment from InfoPath form using C#”

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s