Below code extracts the attachment and store it a share with actual
attachment name. How can I set filename to a text field's value in my
infopath form? So if attached file name is FileA.txt but the value in a text
field (FinalName) in form is FileXYZ.txt, file should be save to share as
FileXYZ.txt
-----------------------------------------------------------------------------------------------------
public void Submit_Clicked(object sender, ClickedEventArgs e)
{
// Write your code here.
XPathNavigator docXN = this.CreateNavigator();
XPathNavigator opnXN = docXN.SelectSingleNode("/my:myFields/my:Attachment",
this.NamespaceManager);
byte[] attachmentNodeBytes = Convert.FromBase64String(opnXN.ToString());
int fnLength = attachmentNodeBytes[20] * 2;
byte[] fnBytes = new byte[fnLength];
for (int i = 0; i < fnBytes.Length; i++)
{
fnBytes[i] = attachmentNodeBytes[24 + i];
}
char[] charFileName = System.Text.UnicodeEncoding.Unicode.GetChars(fnByt es);
string fileName = new string(charFileName);
fileName = fileName.Substring(0, fileName.Length - 1);
byte[] fileContents = new byte[attachmentNodeBytes.Length - (24 +
fnLength)];
for (int i = 0; i < fileContents.Length; ++i)
{
fileContents[i] = attachmentNodeBytes[24 + fnLength + i];
}
// Below is for storing to a Share
FileStream fs = new FileStream(@\\Server\EmpFiles\ + fileName,
FileMode.Create);
fs.Write(fileContents, 0, fileContents.Length); fs.Close();
----------------------------------------------------------------------------------------------------