How to submit an attachment - InfoPath Dev

InfoPath Dev

Use our Google Custom Search for best site search results.

How to submit an attachment

Last post 02-19-2014 12:02 PM by esteuardo. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 04-01-2009 01:41 PM

    How to submit an attachment

    I've read through all of the articles on this forum about how to save a file attachment from an infopath form, but i havent yet been able to really get it to work.  My form already has a whole bunch of C# code in it, so the qDabra rules won't work.  I've tried to use the examples:

    but can't get either one to work as i dont have access to the sharepoint server to get the microsoft.sharepoint.dll file to complile the code.

    I found this article, which i can get to work, but it saves the attachment to a folder on the local machine.  I'd love to tweak this one a little to have it save to a document library rather than the local disk.  The form submit the info to a sharepoint list, if i could get the attachment to attach to that list item that would be best, but if not, at least submit the attachment to a document library and have a way to link the two so that price quotes will be attached to the request form.

    Here is the code i'm using that is working thus far:

    XPathNavigator docXN = this.CreateNavigator();

    XPathNavigator opnXN = docXN.SelectSingleNode

    ("/my:myFields/my:attachment", this.NamespaceManager);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[20] * 2;

    byte[] fnBytes = new byte[fnLength];

    // The actual filename starts at position 24 . . .

    for (int i = 0; i != fnLength; 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\" + fileName, FileMode.Create);

    fs.Write(fileContents, 0, fileContents.Length); fs.Close();

  • 01-23-2010 01:12 AM In reply to

    Re: How to submit an attachment

    Hi there,

    I've been trying to achieve a similiar thing myself, and as far as I understand qDabra is the only way to do it (as you can't upload files directly through infopath or sharepoint as it is). I believe there is a way to do this using K2's SmartObjects, but i haven't tried this myself.

    Maybe a sneaky way could be to establish a web service you can call, submitting the base64 data and the web service itself saves it as a file?

    Let me know if you find a solution, i'd be very interested to see one :)

  • 02-19-2014 12:02 PM In reply to

    Re: How to submit an attachment

    Did you ever get this to work?

    I was able to get it to work with the following code:

    public void CTRL35_5_Clicked(object sender, ClickedEventArgs e)


    // Write your code here.

    XPathNavigator mainDS = MainDataSource.CreateNavigator();

    string base64EncodedString = mainDS.SelectSingleNode(

    "my:myFields/my:Attachment", NamespaceManager).Value;

    if (!String.IsNullOrEmpty(base64EncodedString))



    InfoPathAttachmentDecoder decoder =

    new DecoderInfoPathA.InfoPathAttachmentDecoder(base64EncodedString);



    @\\Your\Path\Here + decoder.Filename, decoder.DecodedAttachment);



    The only problem is that it will only work when the InfoPath form is in full trust...

    I was hoping you may know a way to code in C# to have it have full trust when using InfoPath in client version only.

Page 1 of 1 (3 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.