dreagan
dreagan

Reputation: 2446

coldfusion refuses to decode base64 image

A couple of hours ago I found that I could easily communicate between the html5 file api and coldfusion by passing along the DataUrl created by the file api through form attributes and let coldfusion write the image file for me to the server.

This worked fine.

Now however, it refuses to decode these base64 strings I'm passing along. I've already tested the base64 strings in other decoders and they display the image correctly. I haven't changed anything to the code. So can anyone please tell me what's going on here..?

<cfimage source="#attributes.image#" action="write" destination="../images/new_image.png" isBase64="yes" overwrite="true">

edit 20/12: Today it started working again when I turned on my computer at work. It is still not clear to me what happened yesterday. The error Coldfusion returned was: 'the file does not appear to be base64-encoded', which it absolutely was, I checked more than once. The base64-string I passed to the cfimage tag contained headers just like you described.

Upvotes: 2

Views: 2267

Answers (1)

Mike Causer
Mike Causer

Reputation: 8314

You could skip the <cfimage> tag and use a <cffile> tag if all you are doing is saving it exactly as is. <cfimage> adds a bunch of overhead and is only useful if you plan to modify the image before saving it.

Eg.

<cffile action="write" file="C:\temp\test-image.jpg" output="#binaryDecode(base64data)#" addnewline="no">

Alternatively try...

<cfset myImage = imageReadBase64(base64data)>
<cfimage action="write" source="#myImage#" destination="C:\temp\test-image.jpg">

Does your base64 string have headers?

eg. "data:image/jpg;base64,..." at the start of the string?

From memory, imageReadBase64() supports base64 strings with and without headers.

http://www.cfquickdocs.com/cf9/#BinaryDecode

http://www.cfquickdocs.com/cf9/#ImageReadBase64

Upvotes: 3

Related Questions