ColdFusion Fax - Retrieve Inbound Fax Image

In this sample, the GetImageChunk method is used to retrieve an image for an inbound (received) fax with ColdFusion.

In order to retrieve the fax image, you need the fax message ID and file size, both of which are retrieved using the GetList method.

This ColdFusion fax snippet shows you how to retrieve a fax image in multiple chunks (the chunk size is configurable):


<cfoutput>

<!--- Username and password for the Interfax account. --->
<cfset interfaxUsername = "">
<cfset interfaxPassword = "">

<!---
    Size in bytes of the inbound message. This value
    is returned by method GetList, which should be run
    before this one.
--->
<cfset message_size = 10000>
<cfset MessageID = "29725616">
<cfset MarkAsRead = false>
<cfset ChunkSize = 100000>
<cfset imageChunk = ToBinary("")>
<cfset file_type = "">
<cfset resultImage = ToBinary("")>


<!--- Create and call the web service --->
<cfset faxWebService = CreateObject("webservice", "https://ws.interfax.net/inbound.asmx?wsdl")>

<cfloop from="0" to="#message_size - 1#" step="#ChunkSize#" index="ChunkIndex">

    <cfset getImageChunkResult = faxWebService.GetImageChunk(interfaxUsername, interfaxPassword, MessageID, MarkAsRead, ChunkSize, ChunkIndex, "imageChunk")>

    <cfif getImageChunkResult eq 0>

        <cfscript>
            filesBuffer = createObject("java", "java.io.ByteArrayOutputStream").init();
            filesBuffer.write(resultImage, 0, Len(resultImage));
            filesBuffer.write(imageChunk, 0, Len(imageChunk));

            resultImage = filesBuffer.toByteArray();
            filesBuffer.close();
        </cfscript>

    <cfelse>

        Problem retrieving image chunk #ChunkIndex# on error #getImageChunkResult#

    </cfif>
</cfloop>


<cfif resultImage[1] eq Asc("I") and resultImage[2] eq Asc("I") and resultImage[3] eq Asc("*")>
    <!--- TIFF files start with "II*" --->
    <cfset file_type = "tif">
<cfelseif resultImage[1] eq Asc("%") and resultImage[2] eq Asc("P") and resultImage[3] eq Asc("D") and resultImage[4] eq Asc("F")>
    <!--- PDF files start with "%PDF" --->
    <cfset file_type = "pdf">
<cfelse>
    <cfthrow message="Could not determine image file type.">
</cfif>


<cfset filename = ExpandPath(".") & "/" & MessageID & "." & file_type>
<cffile action="write" file="#filename#" output="#resultImage#">

File Saved


</cfoutput>

Contact us today

Talk to a member of our team about the benefits InterFAX can bring to your organization's communications processes.

Contact us today