CFLib.org – Common Function Library Project

urlBase64Encode(str)

Last updated August 10, 2007

author

Alan McCollough

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Encodes a string to Base64 format, then urlEncodes the result so that it works when used as part of a URL string. The "Base64 then urlEncode" is necessary to convert any reserved chars such as "+" or "=", which would cause problems if used in a URL string.

Return Values:
Returns a string.

Example:

<cfscript>
myVar = "chars like ? would be bad in a URL";
myencodedvar = urlBase64Encode(myVar);
</cfscript>

<cfoutput>#myencodedvar#</cfoutput>

Parameters:

Name Description Required
str String to format. Yes

Full UDF Source:

/**
 * Encodes a string to base64 format, then urlEncodes the result so that it works when used as part of a URL string.
 * 
 * @param str      String to format. (Required)
 * @return Returns a string. 
 * @author Alan McCollough (amccollough@anmc.org) 
 * @version 1, August 10, 2007 
 */
function urlBase64Encode(str){
    
    /* encodes a string to base64 format,
    then urlEncodes the result so that it
    works when used as part of a URL string */
    
    var result = "";
    
    /* convert string to base64 format */
    result = toBase64(str);
    
    /* urlEncode to convert base64 chars that do not work when rendered in a URL 
    Note that this uses the single-argument format to work with earlier versions of CF. */
    result = urlEncodedFormat(result);
    
    return result;
};

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson