Why am I getting an error trying to convert HTML toBinary in ColdFusion when a page is requested by IE6?
-
13-07-2021 - |
سؤال
I'm running ColdFusion on the backend and wrapping my pages like this:
<cfsavecontent variable="renderedResults"><p>hello</p></cfsavecontent>
<cfscript>
compressedHTML = reReplace(renderedResults, "\>\s+\<", "> <", "ALL");
compressedHTML = reReplace(compressedHTML, "\s{2,}", chr(13), "ALL");
compressedHTML = reReplace(compressedHTML, "\s{2,}", chr(09), "ALL");
variables.alredayBinary = "false";
</cfscript>
<cfif cgi.HTTP_ACCEPT_ENCODING contains "gzip">
<cfinvoke component="services.utils" method="gzip" stringToZip="#compressedHTML#" returnvariable="compressedHTML"></cfinvoke>
<cfheader name="Content-Encoding" value="gzip">
<cfset variables.alredayBinary = "true">
</cfif>
<cfheader name="Content-Type" value="text/html; charset=ISO-8859-1">
<cfheader name="Content-Length" value="#len(compressedHTML)#" >
<cfif variables.alredayBinary EQ "false">
<cfcontent reset="no" variable="#ToBinary(compressedHTML)#" />
<cfelse>
<cfcontent reset="no" variable="#compressedHTML#" />
</cfif>
<cfreturn />
Although this makes sense to me and seems to work fine on latest browsers, I just had a user produce a flurry of error message using Internet Explorer 6.
The message I'm getting is:
The parameter 1 of function ToBinary, which is now <HTML STRING> must be a Base-64 encoded string
which tells me, I'm ending up with variables.alredayBinary = false
which should convert the HTML string into a binary encoded string.
Question:
I'm not sure I understand what toBinary
does. Isn't it just for that - taking HTML and converting it? So why the error? Why on IE6 only? I could only test IE8 which is working fine.
Thanks for some hints!
المحلول
Going by what the error message is reporting, try supplying a base64 string to ToBinary
.
<cfcontent reset="no" variable="#ToBinary(ToBase64(compressedHTML))#" />
Note that Adobe recommends against using ToBinary
and ToBase64
and instead recommends using BinaryDecode
and BinaryEncode
instead. However, for your usage you can just use CharsetDecode
instead.
<cfcontent reset="no" variable="#CharsetDecode(compressedHTML, "iso-8859-1")#" />
Note that this would affect more than IE6. Any browser that didn't accept
gzip
would hit that if statement.
Additional note, in my example I set the encoding to iso-8859-1
as per your code, but would suggest you consider utf-8
.