CF9 Converting a string to Binary

New Here ,
Sep 14, 2017 Sep 14, 2017

Copy link to clipboard

Copied

I would like to know why these two values are different.

What is the correct way to convert a string to binary.

<cfhttp url="https://image.freepik.com/free-icon/apple-logo_318-40184.jpg" method="get" getasbinary="yes" result="result">

<cfhttp url="https://image.freepik.com/free-icon/apple-logo_318-40184.jpg" method="get" getasbinary="no" result="result2">

<cfdump var="#result.fileContent#">

<cfdump var="#toBinary(toBase64(result2.fileContent.toString()))#"><cfabort>

<cfheader name="Content-Disposition" value="inline; filename=logo.jpg">

<cfcontent type="image/jpeg; charset=iso-8859-1" variable="#result.fileContent#">

In my real world example, I am actually doing this with FileRead and I use FileRead and not FileReadBinary (but the same problem exists) because I need to crop out a couple of blanks lines in a physical file before converting it to binary, and that file is created by a third party software which I cannot alter.

Can anyone help?

Thanks in advance,

Allan

P.S. I also tried charsetDecode( result2.fileContent.toString(, "utf-8" ) but with the same result as toBinary(toBase64(result2.fileContent.toString()))

Views

917

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Advocate , Sep 15, 2017 Sep 15, 2017
You want to be getting the Byte Array of the fileContent rather then String.Using getAsBinary attribute returns the Byte Array (Binary) value of the response, so by using the toByteArray() function instead of toString() you get the same content back.You don't need to use toBinary either as the Byte Array is the binary result.<cfdump var="#result2.fileContent.toByteArray()#"><cfabort>https://trycf.com/gist/8eb85bf333996a65312b352ff63b91eb/acf2016?theme=monokai

Likes

Translate

Translate
Advocate ,
Sep 15, 2017 Sep 15, 2017

Copy link to clipboard

Copied

LATEST

You want to be getting the Byte Array of the fileContent rather then String.

Using getAsBinary attribute returns the Byte Array (Binary) value of the response, so by using the toByteArray() function instead of toString() you get the same content back.

You don't need to use toBinary either as the Byte Array is the binary result.

<cfdump var="#result2.fileContent.toByteArray()#"><cfabort>

https://trycf.com/gist/8eb85bf333996a65312b352ff63b91eb/acf2016?theme=monokai

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines