Skip to main content
Inspiring
June 10, 2011
Answered

Byte Array Base64 Binary

  • June 10, 2011
  • 1 reply
  • 6478 views

I'm currently working on code to consume a soap webservice in cf8. I have most of the

implementation working and am able to use several of the methods defined in this

particular WS without issue. I'm having a problem with a particular function that

sends a PDF document to the WS. In the WS docs it lists the document 'content'

field as needing to be base64binary and from talking to the developers they say it

specifficly needs to be a byte array of base64binary.

Here's a snippet of the coldfusion code I'm using:

<cffile action="readbinary" file="#arguments.filepath#" variable="documentBinaryData"/>

<cfsavecontent variable="soapBody">
<cfoutput>
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <AddDocument xmlns="http://idoccentral.com">
      <IKey>#arguments.iKey#</IKey>
      <documentDetail>
        <Source>#arguments.source#</Source>
        <FolderID>#arguments.folderID#</FolderID>
        <DocumentID>0</DocumentID>
        <DisplayName>#arguments.displayName#</DisplayName>
        <DocumentDescriptionID>0</DocumentDescriptionID>
        <DocumentDescription>#arguments.documentDescription#</DocumentDescription>
        <FileType>#arguments.fileType#</FileType>
        <Content>#BinaryEncode(documentBinaryData,"Base64")#</Content>
      </documentDetail>
    </AddDocument>
  </soap:Body>
</soap:Envelope>
</cfoutput>
</cfsavecontent>

<cfhttp url="http://stage.doccentral.trpoint.com/DCWebService/IDCService.asmx" method="post" result="httpResponse" timeout="600" throwonerror="yes">
     <cfhttpparam type="header" name="SOAPAction" value="http://idoccentral.com/AddDocument"/>
     <cfhttpparam type="header" name="accept-encoding" value="no-compression"/>
     <cfhttpparam type="xml" value="#trim(soapBody)#"/>
</cfhttp>

The WS response is telling me that everything was ok, I can see the file on the

remote system but when i try to view it from there i get a corrupt file as if my encoding

is incorrect or they are not decoding it correctly.

I've used this same method before for a different web service that also recieves

base64binary encoded PDF documents and it's working just fine there.

As far as i can tell im encoding it to base64Binary correctly, not sure about the

byte array part but im assuming thats whet the cffile readbinary is returning.

Am i doing something wrong in my code or is it more likely to be something on

their end or something wrong in my header?

This topic has been closed for replies.
Correct answer -__cfSearching__-

Yeah I'm not doing anything fancy with the WS, just basically authenticating

and then calling the addDocument method to send the PDF content.

Their system only accepts PDF format, the file type param is useless, so i haven't

tried any other file types. I have tried it with multiple different PDF files just to make

sure it wasn't a broken or non standard file coming from my end.

I just did a compare on file size and the original file was 123k, resulting file on

their server when i download it is only 545 bytes so yeah it's completly fubar at

that point. You would think even with an improper encode or decode that the

result file would be a lot bigger then that.

Do you think maybe i need to pass a content length in my header? The reason

I'm doing the whole cfsavecontent cfhttp method is because i tried it as a cfInvoke

on the webservice and couldn't manage to get it working by simply building out a

struct to pass in and letting CF handle all the XML internaly, it would always come

back and tell me that it couldn't find a method called addDocument with those

params. The cfinvoke webservice is however working ok for the other WS method

i need to call in order to get my auth key.

I'm beginning to think the problem lies on their end, it's been very difficult and slow

going to get ahold of somebody over there to help me though. The main point of this

posting was to find out if my encoding method was wrong, from what i can tell theres

not really another way to do that encoding in CF though.


it is only 545 bytes

Hmm... are the resulting files always that small? Crazy thought, but open the tiny file with notepad or something. Just to see if even looks like a truncated pdf. They usually start with %PDF-(version number).

Also you might sniff the traffic to see what your full soap request looks like. See how it compares to their sample

http://stage.doccentral.trpoint.com/DCWebService/IDCService.asmx?op=AddDocument

The main point of this posting was to find out if my encoding method was wrong

Unless I am misunderstanding what is meant by base64binary.. I do not think so. Your code looks correct to me.

The reason I'm doing the whole cfsavecontent cfhttp method is because i tried it as a cfInvoke on the webservice and couldn't manage to get it working by simply building out a struct to pass in and letting CF handle all the XML internaly, it would always come back and tell me that it couldn't find a method called addDocument with those params.

Out of curiosity, what did you try? It does not look too complicated (famous last words I know ...)

1 reply

Inspiring
June 10, 2011

...the developers they say it specifficly needs to

be a byte array of base64binary.

Something about that description feels "off". But if you mean an array of bytes converted to a base64 string, nothing about your code jumps out at me as being glaringly wrong.

The WS response is telling me that everything was ok,

Does the content-length line match up with your request size?

when i try to view it from there i get a corrupt file

How are you accessing the file, directly or with another web service call? Could the problem be with the code used to view/retrieve the file?

Message was edited by: -==cfSearching==-

zeejayyAuthor
Inspiring
June 10, 2011

This is for an MLS Realtor service. It allows them to put their documents up so their clients can access them easily.

I have a development account on the staging server that i can log into and download the files. Seems to be just a simple http file download.

I'll have to check the content length thats something i haven't really had to look at before.

I've done a couple of web service projects in CF but im still learning some of the ins and outs.

Inspiring
June 10, 2011

log into and download the files that i have sent in. Seems

to be just a simple http file download.

Oh, I thought it might be something more complex. I still do not see anything obviously wrong with the code. Unless the definition of byte array base64binary is different than what we are thinking it is ..

Did you check the basics like file size? How does the final file compare to the original - smaller, larger, same .. ? Also, I assume the issue occurs with every file, not just certain types, etcetera... ?