Author Topic: ColdFusion code for remote export of data  (Read 1979 times)

MEngelbyPQ

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 21
    • View Profile
ColdFusion code for remote export of data
« on: December 10, 2016, 04:40:48 am »
One of the very nice things about this library is how quickly one can be up and running.  I was a bit dismayed when ColdFusion wasn't there.  After a few false starts, I quickly got this up and going.  If you find it useful you can add it to the server side code examples.

Code: [Select]
<!--- ColdFusion Version 9 Code --->
<cfif CGI.REQUEST_METHOD EQ "POST">

    <!--- Get the HTTP request body content.
        NOTE: Use toString() as an intermediary method call since
        the payload comes across as a byte array (binary data)
        which needs to be turned back into a string before
        ColdFusion can parse it --->
    <cfset requestBody = toString( getHttpRequestData().content ) />
    <!--- At this point, requestBody is a string that resembles URL parameters --->
    <cfset requestParams = StructNew()>
    <cfloop index="nameValuePair" list="#requestBody#" delimiters="&">
        <cfset requestParams["#ListFirst(nameValuePair, '=')#"] = "#ListLast(nameValuePair, '=')#">
    </cfloop>

    <!--- Save the data into an active session along with the filename --->
    <cfset StructDelete(Session, "exportData")>
    <cfset StructDelete(Session, "exportFilename")>
    <cfif IsDefined("requestParams.pq_data") AND IsDefined("requestParams.pq_ext")>
        <cfif ListContains("csv,htm,json,xlsx,zip", "#requestParams.pq_ext#", ",")>
            <!--- Data coming across is always URLEncoded to ensure accurate transmission --->
            <cfset data = URLDecode(requestParams.pq_data)>
            <cfif IsDefined("requestParams.pq_decode") AND UCase(requestParams.pq_decode) eq "TRUE">
                <cfset data = BinaryDecode(data, "Base64")>
            </cfif>
            <cfset Session.exportData = data>
            <cfif IsDefined("requestParams.pq_filename")>
                <cfset Session.exportFilename = requestParams.pq_filename & '.' & requestParams.pq_ext>
                <cfoutput>#Session.exportFilename#</cfoutput>
            </cfif>
        </cfif>
    </cfif>
</cfif>

<cfif CGI.REQUEST_METHOD EQ "GET">

    <!--- The payload for a GET is URL parameters --->
    <cfif IsDefined("URL.pq_filename") AND Session.exportFilename eq URL.pq_filename>
        <cfif IsDefined("Session.exportData")>
            <cfset data = Session.exportData>
            <cfset StructDelete(Session, "exportData")>
            <cfheader name="Content-disposition" value="attachment;filename=#URL.pq_filename#">
            <cfcontent type="application/octet-stream" variable="#ToBinary(ToBase64(data))#">
        </cfif>
    </cfif>
</cfif>


paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6307
    • View Profile
Re: ColdFusion code for remote export of data
« Reply #1 on: December 12, 2016, 05:41:41 pm »
Thanks for sharing, it's greatly appreciated!