Search code examples
web-servicesvb6mtom

How to download an MTOM attachment in VB6


Please, someone can show me an example about how to download an MTOM attachment and save it to disk using VB6?

At the moment I am able to get a response from my ws, using this code:

Dim objDom As Object
Dim objXmlHttp As Object
Dim strRet As String
Dim intPos1 As Integer
Dim intPos2 As Integer
On Error GoTo Err_PW
' Create objects to DOMDocument and XMLHTTP
Set objDom = CreateObject("MSXML2.DOMDocument")
Set objXmlHttp = CreateObject("MSXML2.XMLHTTP")

'Set objXmlHttp = New MSXML2.XMLHTTP40
' Load XML
objDom.async = False
objDom.loadXML XmlBody

' Open the webservice
objXmlHttp.Open "POST", AsmxUrl, False
' Create headings
objXmlHttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
objXmlHttp.setRequestHeader "SOAPAction", SoapActionUrl

' Send XML command
objXmlHttp.Send (objDom.xml)
' Get all response text from webservice
strRet = objXmlHttp.responseText

But from this point I don't know how to proceed to grab the MTOM attachment.

This is what return from ws:

--uuid:0b9372cf-63bb-484f-87f3-7bedfaa776fb
Content-Id: <rootpart*0b9372cf-63bb-484f-87f3-7bedfaa776fb@example.jaxws.sun.com>
Content-Type: application/xop+xml;charset=utf-8;type="text/xml"
Content-Transfer-Encoding: binary

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body><ns2:getContentStreamResponse xmlns:ns2="http://docs.oasis-open.org/ns/cmis/messaging/200908/" xmlns="http://docs.oasis-open.org/ns/cmis/core/200908/">
<ns2:contentStream>
<ns2:length>2572861</ns2:length>
<ns2:mimeType>application/vnd.openxmlformats-officedocument.wordprocessingml.document</ns2:mimeType>
<ns2:filename>SF-MASTER - SDTC.docx</ns2:filename>
<ns2:stream>
<xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:4b7c8c60-fc9e-4da8-92fc-aa8c2fdfb401@example.jaxws.sun.com"/></ns2:stream>
</ns2:contentStream>
</ns2:getContentStreamResponse>
</S:Body>
</S:Envelope>
--uuid:0b9372cf-63bb-484f-87f3-7bedfaa776fb
Content-Id: <4b7c8c60-fc9e-4da8-92fc-aa8c2fdfb401@example.jaxws.sun.com>
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
Content-Transfer-Encoding: binary

PK
31/05/2018 10:04:06: UUID documento: 0b9372cf-63bb-484f-87f3-7bedfaa776fb
31/05/2018 10:04:06: Tag finale: --uuid:0b9372cf-63bb-484f-87f3-7bedfaa776fb--
31/05/2018 10:04:06: lunghezza documento: 2572861

Thank you very much


Solution

  • Use this as a guide and modify your code to save the returned file data as a binary file.

    Add to top of function

    Const adTypeBinary = 1
    Const adSaveCreateOverWrite = 2
    
    Dim varByteArray, ado
    Dim strFile as String
    

    After you send the request insert this code

    If objXmlHttp.Status <> "200" Then
        Exit Function
    End If
    
    ' Pull this from returned header data
    strFile = "SF-MASTER - SDTC.docx"
    
    ' get file contents
    varByteArray = objXmlHttp.ResponseBody
    
    Set objXmlHttp= Nothing
    
    'Now save the binary file
    On Error Resume Next
    Set ado = Nothing
    Set ado = CreateObject("ADODB.Stream")
    ado.Type = adTypeBinary
    ado.Open
    ado.Write varByteArray
    ado.SaveToFile strFile, adSaveCreateOverWrite
    ado.Close