Encode/Decode File Attachment From Form Template 1 To Form Template 2 - InfoPath Dev
in

InfoPath Dev

Encode/Decode File Attachment From Form Template 1 To Form Template 2

Last post 06-18-2014 09:48 AM by NickCalifornia. 5 replies.
Page 1 of 1 (6 items)
Sort Posts: Previous Next
  • 06-16-2014 06:35 PM

    Encode/Decode File Attachment From Form Template 1 To Form Template 2

    Hi all,

     I've read quite a bit on this, but am finally feeling stumped.  The basic scenario is I have a file attachment in 1 form template that I'd like to be able to programmatically attach in a different form template.

    I have both attachment fields; AttachmentStartField, AttachmentDestination; available through either a secondary data source or main fields (myFields):

    I've read over articles about utilizing a class for an ecoder/decoder, but it doesn't quite address decoding an existing attached file from a node and then encoding that file to a new node by using "SetValue".  Thoughts/suggestions?

    Dim MainNav As XPathNavigator = Me.CreateNavigator()

    Dim SecNav As XPathNavigator = Me.DataSources("XML Connection").CreateNavigator()

    Dim DecodeMS As New MemoryStream(Convert.FromBase64String(SecNav.SelectSingleNode("/ns1:myFields/ns1:AttachmentStartField", NamespaceManager).ToString()))

    Dim DecodeMSByte As Byte() = DecodeMS.ToArray()

    '''''

    '''''''''''''' What steps do I need to tweak to convert it to the appropriate format/etc

    'to accept in the AttachmentDestination field? As it stands I get non-data type errors with this code. Thanks!

    '''''

    MainNav.SelectSingleNode("/my:myFields/my:AttachmentDestination", NamespaceManager).SetValue(Convert.ToBase64String(DecodeMSByte))

  • 06-17-2014 11:03 AM In reply to

    Re: Encode/Decode File Attachment From Form Template 1 To Form Template 2

    Ok, so I think the main issue here is since I am trying to add a file attachment from a secondary data source, the information actually does not carry over since the only field I can actually promote is a "count" field of the XML entry.  I think that may be the issue and perhaps I need to build this attachment string in the source data source first, then use that to build any attachments out in the new form template?

  • 06-17-2014 03:05 PM In reply to

    Re: Encode/Decode File Attachment From Form Template 1 To Form Template 2

    Ok, wow, another update.  I was able to transfer this over if I physically pre-populated the target destination with any type of file attachment; i.e. this works if I pre-populate the field (also, ignore my second post, the data is available from the XML source document).

    :::::::::::::::::::: 

    MainNav.SelectSingleNode("/my:myFields/my:RepeatingGroup[1]/my:DestinationField", NamespaceManager).SetValue( _

    SecNav.SelectSingleNode("/ns1:myFields/ns1:RepeatingGroup[1]/ns1:SourceField", NamespaceManager).Value)

    :::::::::::::::::::: 

    I guess it has something to do with the "nil" attribute; I've been able to use "MoveToAttribute" to detect the nil attribute and then delete the node (like all the examples out there say).  But my issue is now re-adding the node correctly since there won't be any scenario where I want the user to pre-populate the destination field.

     Thoughts anyone?

  • 06-17-2014 03:49 PM In reply to

    Re: Encode/Decode File Attachment From Form Template 1 To Form Template 2

    Well after messing around with this for a while I've finally found a solution.  Again, tying back to the "nil" issue; deleting this node was causing some issues when I attempted to actually apply the value over after removing it.  I decided to copy the examples I found exactly (they passed the specific node into a sub, whereas I was using DeleteSelf directly on the node path before passing it).  This subtle change apparently makes it all work; Here's the code in case anyone cares :).

     

    If MainNav.SelectSingleNode("/my:myFields/my:RepeatingGroup1/my:DestinationField", NamespaceManager) _

    .GetAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance").ToString() = True Then

    Dim MyValue As XPathNavigator = MainNav.SelectSingleNode("/my:myFields/my:RepeatingGroup1/my:DestinationField", NamespaceManager)

    DeleteNil(MyValue)

    Dim MyValueByte() As Byte = Convert.FromBase64String(SecNav.SelectSingleNode("/ns1:myFields/ns1:RepeatingGroup2/ns1:SourceField", NamespaceManager).ToString())

    MyValue.SetValue(Convert.ToBase64String(MyValueByte))

    End If

    End Sub

    Public Sub DeleteNil(ByVal node As XPathNavigator) If (node.MoveToAttribute( _

    "nil", "http://www.w3.org/2001/XMLSchema-instance")) Then

    node.DeleteSelf()

    End If

    End Sub

     

  • 06-17-2014 05:56 PM In reply to

    Re: Encode/Decode File Attachment From Form Template 1 To Form Template 2

    I'm sorry you ended up talking to yourself here, but I'm glad you figured it out & thanks for posting your solution.

    See how in DeleteNil in the if statement the node is being moved to the nil attribute and then DeleteSelf() is called? The node at that point is actually the nil attribute...

    Hilary Stoupa

  • 06-18-2014 09:48 AM In reply to

    Re: Encode/Decode File Attachment From Form Template 1 To Form Template 2

    That's OK, at least someone with a similar issue may find this thread useful ;).  Also, I refined the code a bit more once I wrote it to iterate through all possibilities of a repeating table field containing a file attachment.  Through this process I also realized that essentially "moving to the attribute" is the same concept as setting the dimensioned xpathnavigator to that attribute.  Thanks for the help as usual!

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