3 Replies Latest reply on Dec 1, 2006 10:52 AM by Umbrae

    Output Binary data to browser as file

    Umbrae
      I know this has been covered a thousand time, but every example I am given does not work.

      I am accessing a Java WebService to get a document (in this case PDF). This WS has no WDSL, so I am hitting it through CFHTTP. The WS returns me a hashmap with error codes etc. One element is a Binary string.

      I have done everything imaginable and the most I can do is get the raw text to display. Unfortunately, saving a temp file then using CFFILE is not an option for security reasons; however, if I try this the file is corrupted and will not open. When I use CFHEADER I get a complaint about java.lang.String. However, ToBinary gives me a CF error with garbage text and no real error, and BinaryDecode always whines about input and output encodings not matching.

      This is what I use the call the service:
      <CFHTTP URL="HIDDEN" METHOD="GET" RESULT="testFN" CHARSET="ISO-8859-1"></CFHTTP>

      And these are somethings I have tried:

      <cfheader name="Content-Type" value="pdf">
      <CFHEADER NAME="Content-Disposition" VALUE="inline; filename=my.pdf">
      <CFCONTENT TYPE="application/pdf" VARIABLE="#BinaryDecode(testFN.Filecontent, "Base64")#">

      <cfscript>
      context = getPageContext();
      context.setFlushOutput(false);
      response = context.getResponse().getResponse();
      out = response.getOutputStream();
      response.setContentType("application/pdf");
      response.setContentLength(arrayLen(testFN.Filecontent));
      out.write(testFN.Filecontent);
      out.flush();
      out.close();
      </cfscript>

      <cfcontent type="application/pdf; charset=ISO-8859-1">
      <CFSCRIPT>
      writeOutput(toString(testFN.Filecontent));
      </cfscript>

      None work. I either get a blank page, a CF error, or plain text. Other Java application use this same WS without issue, so any complaints about the WS have been ignored.

      I need help badly. I am not sure what else to try.
        • 1. Re: Output Binary data to browser as file
          MikerRoo Level 1

          This should be all you need (note the subtle changes):

          <cfheader name="Content-Disposition" value="attachment;filename=my.pdf">
          <cfcontent type="application/pdf" reset="Yes" variable=#BinaryDecode (testFN.Filecontent, "Base64")#>

          <CFABORT> <!--- Just in case there is any OnRequestEnd code. --->


          EXCEPT that it is not entirely clear that the entire filecontent is supposed to be a pdf file.

          Please post a link to a page that shows a cfdump of testFN.
          Or provide enough information for us to do our own cfhttp call to the service.

          You can private message me if need be.
          • 2. Output Binary data to browser as file
            Umbrae Level 1
            Thanks for the reply.

            I have had problems using BinaryDecode. It states the input and output are not the same not matter what I use as the binaryencoding. I can get a pdf binary object sent to the browser by using this:

            <CFHEADER NAME="Content-Disposition" VALUE="attachment; filename=#fileNAME#.#fileEXT#">
            <CFCONTENT TYPE="*/*" VARIABLE="#ToBinary(ToBase64(docBINARY))#">

            ToBinary nor ToBase64 work on their own (I get an error about ByteArrays), but they seem to work together. However, now I get a 12 page PDF with 1 blank page and all the other pages generate errors in the Adobe Reader. I have confirmed that the document comes up fine in the Java applications that do basically the same thing, but it is like ColdFusion does not understand the data type.

            Unfortunately, our DEV and UAT systems are not exposed for me to give you access. The Web Service I am consuming will never be available externally, which is why I am building a CF passthrough to display the documents to public users. The internal system will output private contracts, so there are privacy issues related to opening it up. Our prod deployment is very strict, so I cannot even put up public pages for testing until all involved sign off on functionality.

            Obviously, the CFDUMP exceeds the text limit, so I cannot post it either. :(

            Edit: Also, I found out that the first byte of object is a status code. I have parsed that off (why you now see docBINARY), but I only see this as a square and have not been able to convert this to the number code I should be getting.
            • 3. Re: Output Binary data to browser as file
              Umbrae Level 1
              Ok, I think I have it now. When I tell CFHTTP to GETASBINARY I do not have to do any conversions on the file. I just dump FileContent into CFCONTENT and BOOM.

              Of course, I loose my error code, but it does not appear to mess up the document. I should be able to dump the thing in a CFTRY and CFCATCH to catch if a document is not sent; I will just not have any idea why the document failed to load. This should not be a show stopper though.

              Now I have to handle the other part which is unzipping the XLS files. ICK