Hi,
I have been banging my head against this for the whole weekend now. We have a project where we need to use Infopath 2003 to submit data to a SharePoint list (not the issue here). The form also needs to append a file to the list item (the issue). There is a web service in Sharepoint, AddAttachment, where I can pass a Base64 encoded string and Sharepoint will save the a new attachment. The problem is that the file attachment control has a variable length header infront of the actual file.
I have been able to get the filename, and other information out from the header (see code below), but the actual problem is that I need to strip the header away and still maintain the Base64 encoding doing this in VBScript or Jscript.
Any suggestions?
Regards,
// Henrik
Code to extract filename:
'File control name:
Set node = XDocument.DOM.documentElement.selectSingleNode("/my:myFields/my:field1")
If(node.text <> "") Then
'typecast the node to base64 as Infopath doesn't do this in node definition
node.DataType = "bin.base64"
'convert base64 node value to binary and store in nodeValue byte array
nodeValue = node.nodeTypedValue
'Convert the bytearray to hex
dim convertByteArrayToHexString
convertByteArrayToHexString = ""
For intCounter = 1 to LenB(nodeValue)
convertByteArrayToHexString = convertByteArrayToHexString & Right("0" & Hex(AscB(MidB(nodeValue, intCounter, 1))), 2)
Next
'Extract the filename buffer size first (the filename buffer has a different size depending on length of the filename)
'41 and 47 is the position in the header where this information is located. 41 is actaully position 20 in the bytearray (20*2) + 1(arrays in vbscript starts on 1)
filenameBuffer = getFilenameBuffer(convertByteArrayToHexString,41,47)
'And then the actual filename
'49 is the start position for the filename, the end is calculated
filename = getFileName(convertByteArrayToHexString,49,filenameBuffer*4 + 49 - 2)
Function getFilenameBuffer(data,startLoop,stopLoop)
dim k
dim filenameBuffer
for k = startLoop to stopLoop step 2
dim y1
dim y2
dim y3
y1 = Mid(data,k,2)
y2 = CLng("&h" & y1)
y3 = y3 + Int(y2)
filenameBuffer = y3
next
getFilenameBuffer = filenameBuffer
End Function
Function getFileName(data,startLoop, stopLoop)
dim k
dim filename
for k = startLoop to stopLoop step 4
dim h1
dim h2
dim h3
h1 = Mid(data,k,2)
h2 = CLng("&h" & h1)
h3 = Chr(h2)
if(h3 <> "") then
filename = filename & Cstr(h3)
end if
next
getFileName = filename
End Function