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, which is 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):


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

    Size in bytes of the inbound message.
<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", "")>

<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>

            filesBuffer = createObject("java", "").init();
            filesBuffer.write(resultImage, 0, Len(resultImage));
            filesBuffer.write(imageChunk, 0, Len(imageChunk));

            resultImage = filesBuffer.toByteArray();


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


<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">
    <cfthrow message="Could not determine image file type.">

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

File Saved