House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home /  Groups /  ColdFusion Talk (CF-Talk)

Extracting binary multipart MIME attachments from webservice

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Andrew Clubb
08/01/2006 12:17 PM

I'm successfully retrieving a PDF from an external webservice as a MIME attachment, using <cfhttp> (given the limitations with <cfinvoke> and complex types). However, although I successfully get the webservice response, I can't find a way to write the PDF to disk without corrupting it, due to string encoding issues. The PDF is returned as a multipart MIME attachment in binary (Content-Type: multipart/mixed and Content-Transfer-Encoding: binary), and although I can successfully parse out the bit of the response that is the PDF, this requires converting the returned Java ByteArray to a string to process. At this point, the PDF part of the attachment naturally gets corrupted when being translated from binary. However, writing cfhttp.filecontent out directly to disk, without converting to string, then chopping the unnecesary bits of MIME boundaries etc. in a text editor results in a perfect PDF that opens fine. Is there any way (e.g. using MIME handling in javax.mail.*) to allow me to write a readable PDF. I thought getting the data back from the webservice would be the difficult bit, not getting it on to disk :-( Any assistance much appreciated.

Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Ian Skinner
08/01/2006 12:42 PM

Is it corrupted or do you just need to turn it back into a binary? I have this proof of concept I did recently that reads a PDF file converts into a binary string in a CFC.  This is then passed to a CFM file that converts the string back into binary and writes the file to the disk.  Hopefully something here can give you an idea. Fileserving.cfc --------------- <cfcomponent>   <cffunction name="serveFile" access="remote" returntype="string">     <cfargument name="fileToServe" required="yes" type="string">          <cfset var fileObj = "">     <cfset var returnVar = "">     <cfset var file = "">     <cffile action="readBinary" file="g:\playground\probono\#arguments.fileToServe#" variable="fileObj">     <cfset returnVar = binaryEncode(fileObj,"Base64")>          <cfreturn returnVar>   </cffunction> </cfcomponent> Fileserving.cfm --------------- <cfset myFile="test.pdf"> <cfheader name="Content-Type" value="pdf"> <cfheader name="Content-Disposition" value="attachment; filename=#Myfile#"> <cfset testObj = createObject("webservice", "http://playground/probono/fileServing.cfc?wsdl")> <cffile action="write" file="g:\playground\probono\tempServe.pdf" output="#BinaryDecode(testObj.serveFile(myFile),'Base64')#" addnewline="No" > <cfcontent type="application/pdf" file="g:\playground\probono\tempServe.pdf" reset="no"> -------------- Ian Skinner Web Programmer BloodSource www.BloodSource.org Sacramento, CA --------- | 1 |   | ---------  Binary Soduko |   |   | --------- "C code. C code run. Run code run. Please!" - Cynthia Dunning Confidentiality Notice:  This message including any attachments is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender and delete any copies of this message.

Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Andrew Clubb
08/01/2006 01:47 PM

Thanks. Unfortunately, the webservice I have to work with doesn't return a string, as your version does, it returns a multipart MIME response as below, with the PDF being binary encoded in transmission. What I'm essentially looking for is a way to get just the PDF data from between the %PDF-1.3 and %%EOF, but I can't use any CF string functions as they will turn the returned cfhttp.filecontent (a bytestream) into a string, and the binary part of the response will get corrupted at that point. ------=_Part_2515248_28299851.1152654204502 Content-Type: text/xml; charset=UTF-8 Content-Transfer-Encoding: binary Content-Id: <60C88667A0B2D0B7DD41481F77F0593F> <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">[Removed Envelope data]</soapenv:Envelope> ------=_Part_2515248_28299851.1152654204502 Content-Type: multipart/mixed;   boundary="----=_Part_2515247_17486007.1152654204402" Content-Transfer-Encoding: binary Content-Id: <80381D8FB16DEE9191AE1ED0EB9C9E11> ------=_Part_2515247_17486007.1152654204402 Content-ID: Response Attachment No:1 %PDF-1.3 %âãÏÓ [Lots of binary PDF data here removed] 173 %%EOF ------=_Part_2515247_17486007.1152654204402-- ------=_Part_2515248_28299851.1152654204502--


<< Previous Thread Today's Threads Next Thread >>

Search cf-talk

May 24, 2012

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
     1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31     

Designer, Developer and mobile workflow conference