Hi Devs,
If you want to Programmaticall add attachment to your infopath file. Use the Following Function :
private void btnSelect_Click(object sender, EventArgs e)
{
dlgOpen.ShowDialog();
txtFile.Text = dlgOpen.FileName;
string[] str = txtFile.Text.Split('\\');
fileName = str[str.GetUpperBound(0)].ToString();
}
private void btnAttach_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(txtFile.Text, FileMode.Open);
byte[] fileBuffer = new byte[fs.Length];
fs.Read(fileBuffer, 0, (int)fs.Length);
fs.Close();
// To Fetch the Node
string xmlFilePath = @"E:\Infopath\Form\template.xml";
XMLManager.XMLSearchHelper xmlSrch = new XMLManager.XMLSearchHelper();
xmlSrch.XMLFile = xmlFilePath;
XmlNode n1 = xmlSrch.GetValue("/my:myFields");
XmlDocument myDoc = new XmlDocument();
XmlDocument doc = new XmlDocument();
myDoc.Load(xmlFilePath);
doc.Load(xmlFilePath);
XmlElement attachmentElement = myDoc.CreateElement("FileAttachment");
//Import the last book node from doc2 into the original document.
XmlNode newattach = doc.ImportNode(myDoc.DocumentElement.LastChild,true);
int namebufferlen = fileName.Length * 2;
byte[] namebuffer = new byte[namebufferlen];
byte[] filecontent = new byte[fileBuffer.Length + 24 + namebufferlen];
byte[] filesize = BitConverter.GetBytes((uint)fileBuffer.Length);
byte[] filenamelength = BitConverter.GetBytes((uint)namebufferlen / 2);
filecontent[0] = 199;
filecontent[1] = 73;
filecontent[2] = 70;
filecontent[3] = 65;
filecontent[4] = 20;
filecontent[5] = 0;
filecontent[6] = 0;
filecontent[7] = 0;
filecontent[8] = 1;
filecontent[9] = 0;
filecontent[10] = 0;
filecontent[11] = 0;
filecontent[12] = 0;
filecontent[13] = 0;
filecontent[14] = 0;
filecontent[15] = 0;
filecontent[16] = filesize[0];
filecontent[17] = filesize[1];
filecontent[18] = filesize[2];
filecontent[19] = filesize[3];
filecontent[20] = filenamelength[0];
filecontent[21] = filenamelength[1];
filecontent[22] = filenamelength[2];
filecontent[23] = filenamelength[3];
UnicodeEncoding.Unicode.GetBytes(fileName + "\0", 0, fileName.Length + 1, filecontent, 24);
for (int i = 0; i < fileBuffer.Length; i++)
{
filecontent[24 + namebufferlen + i] = fileBuffer[i];
}
string s = Convert.ToBase64String(filecontent);
attachmentElement.InnerText = s;
myDoc.DocumentElement.SelectSingleNode("my:myFields/my:Req",XMLManager.XMLSearchHelper.nameSpcMgr).AppendChild(attachmentElement);
//n1.AppendChild(attachmentElement);
myDoc.Save(@"C:\Output.xml");
}
}
Thursday, November 1, 2007
Programmatically Attach File to InfoPath
Posted by
Uday
at
12:59 AM
Labels: InfoPath 2007, VisualStudio 2005
Subscribe to:
Post Comments (Atom)




4 comments:
Uday;
This code looks great but I am getting errors when I ty to use this code in the mvsta (embedded visual studio inside infopath2007) am I missing an include statement or something?
This Code used to attach the file to Infopath XSN from windows/web application.
In which line you are getting error.
Verify all the references are added to solution like
System.Xml
System.IO
Hi Uday,
Thank you for this code but I'm new to this and not sure how to use it. All I want to do is rename an attachment that a user has uploaded to the form. Can you help me with this?
Thanks,
Brad
Hey Uday: I have same issue as Brad has. How can I rename an attached file to the value of a text field in the form?
Thanks
Post a Comment