Image field, uploading files.

32 views
Skip to first unread message

Daniel

unread,
Jan 23, 2009, 3:18:45 PM1/23/09
to ColdFusion on Wheels
I'm having a little trouble using the ORM to populate the database
with binary data from a file upload. Am I trying to do something
Wheel's is not ready for?

The exact error message is as follows:

-------------------------------------------------------------------------------------------------
ByteArray objects cannot be converted to strings.

The error occurred in D:\Sites\internal\siteManagement\wheels\model
\crud.cfm: line 895
-------------------------------------------------------------------------------------------------

raulriera

unread,
Jan 24, 2009, 9:03:55 AM1/24/09
to ColdFusion on Wheels
You are probably tryinh to use cffile with params["file" use the
regular form scope

Daniel

unread,
Jan 26, 2009, 8:49:36 AM1/26/09
to ColdFusion on Wheels
As you suggested, I tried using form scope, but that doesn't appear to
make any difference. Can you give me an example of code that is able
to save binary to the database? Here is what I am using. Note that
the "attachment" field is the binary field, and the save() function
produces the error.

<cfset contentDetail = model("contentInline").new(params.contentDetail)
>
<cffile action="UPLOAD" filefield="fileInline"
destination="#getDirectoryFromPath(getCurrentTemplatePath())#"
nameconflict="OVERWRITE">
<cfset contentDetail.attachmentFilename = cffile.ServerFile>
<cfset contentDetail.attachmentMime = "#cffile.ContentType#/
#cffile.ContentSubType#">
<cfset contentDetail.attachmentFilesize = cffile.FileSize>
<cfset contentDetail.attachmentExtension = cffile.ClientFileExt>
<cffile action="readbinary" file="#form.fileInline#"
variable="binaryAttachy">
<cfset contentDetail.attachment = #binaryAttachy#>
<cfset contentDetail.save()>

Chris Peters

unread,
Jan 26, 2009, 10:57:10 AM1/26/09
to cfwh...@googlegroups.com
Try changing the second-to-last line and see if it works (bolded below)...


<cfset contentDetail = model("contentInline").new(params.contentDetail)>
<cffile action="UPLOAD" filefield="fileInline" destination="#getDirectoryFromPath(getCurrentTemplatePath())#" nameconflict="OVERWRITE">
<cfset contentDetail.attachmentFilename = cffile.ServerFile>
<cfset contentDetail.attachmentMime = "#cffile.ContentType#/#cffile.ContentSubType#">
<cfset contentDetail.attachmentFilesize = cffile.FileSize>
<cfset contentDetail.attachmentExtension = cffile.ClientFileExt>
<cffile action="readbinary" file="#form.fileInline#" variable="binaryAttachy">
<cfset contentDetail.attachment = binaryAttachy.toByteArray()>
<cfset contentDetail.save()>

I got that tip from here. You may want to review their approach, although their situation was a little different than yours.
http://www.overset.com/2007/03/02/store-binary-data-in-mysql-with-coldfusion/

Let us know what you come up with!

raulriera

unread,
Jan 26, 2009, 11:35:01 AM1/26/09
to ColdFusion on Wheels
Oh oops, I thought you meant to handle the file upload. My bad :)

On Jan 26, 4:57 pm, Chris Peters <chrisdpet...@gmail.com> wrote:
> Try changing the second-to-last line and see if it works (bolded below)...
>
> <cfset contentDetail = model("contentInline").new(params.contentDetail)>
> <cffile action="UPLOAD" filefield="fileInline"
> destination="#getDirectoryFromPath(getCurrentTemplatePath())#"
> nameconflict="OVERWRITE">
> <cfset contentDetail.attachmentFilename = cffile.ServerFile>
> <cfset contentDetail.attachmentMime =
> "#cffile.ContentType#/#cffile.ContentSubType#">
> <cfset contentDetail.attachmentFilesize = cffile.FileSize>
> <cfset contentDetail.attachmentExtension = cffile.ClientFileExt>
> <cffile action="readbinary" file="#form.fileInline#"
> variable="binaryAttachy">
> *<cfset contentDetail.attachment = binaryAttachy.toByteArray()>*
> <cfset contentDetail.save()>
>
> I got that tip from here. You may want to review their approach, although
> their situation was a little different than yours.http://www.overset.com/2007/03/02/store-binary-data-in-mysql-with-col...
>
> Let us know what you come up with!
>

Daniel

unread,
Jan 26, 2009, 12:13:20 PM1/26/09
to ColdFusion on Wheels
Thanks for the suggestions. That doesn't work for a couple reasons.
It looks like toByteArray is a method from the underyling Java class,
but since I'm using a regular ColdFusion variable, I don't have access
to it. And actually, since my variable is already a Byte Array, I
wouldn't need to convert it anyway.

It seems to me the problem is really a Wheels bug in the $create
function of crud.cfm. It apparently forces an implicit cast to
varchar by trying to determine if the field is null. See the very
last portion of this statement:

loc.param = {value=this[loc.key],
type=variables.wheels.class.properties[loc.key].type,
scale=variables.wheels.class.properties[loc.key].scale, list=false,
null=this[loc.key] IS ""};

If I replace
this[loc.key] IS ""
with




On Jan 26, 10:57 am, Chris Peters <chrisdpet...@gmail.com> wrote:
> Try changing the second-to-last line and see if it works (bolded below)...
>
> <cfset contentDetail = model("contentInline").new(params.contentDetail)>
> <cffile action="UPLOAD" filefield="fileInline"
> destination="#getDirectoryFromPath(getCurrentTemplatePath())#"
> nameconflict="OVERWRITE">
> <cfset contentDetail.attachmentFilename = cffile.ServerFile>
> <cfset contentDetail.attachmentMime =
> "#cffile.ContentType#/#cffile.ContentSubType#">
> <cfset contentDetail.attachmentFilesize = cffile.FileSize>
> <cfset contentDetail.attachmentExtension = cffile.ClientFileExt>
> <cffile action="readbinary" file="#form.fileInline#"
> variable="binaryAttachy">
> *<cfset contentDetail.attachment = binaryAttachy.toByteArray()>*
> <cfset contentDetail.save()>
>
> I got that tip from here. You may want to review their approach, although
> their situation was a little different than yours.http://www.overset.com/2007/03/02/store-binary-data-in-mysql-with-col...
>
> Let us know what you come up with!
>

Daniel

unread,
Jan 26, 2009, 12:18:30 PM1/26/09
to ColdFusion on Wheels
If I replace this[loc.key] IS "" with "false" so that it just
assumes the field is not null, then I can successfully save. So the
question is how to properly evaluate whether a field is null, while
taking into the account that the field may contain binary data.

This definitely is a Wheels bug in my opinion. I'll submit an issue
for it.

Per Djurner

unread,
Jan 26, 2009, 12:22:03 PM1/26/09
to cfwh...@googlegroups.com
Thanks Daniel, let us know if you find a solution for it and we'll
include it in the code for the next release.
/ Per

Daniel

unread,
Jan 26, 2009, 2:43:43 PM1/26/09
to ColdFusion on Wheels
Per,
I think it can be resolved by simply replacing this code:
null=this[loc.key] IS ""
with this code:
null=len(this[loc.key]) eq 0

This code achieves the same result, but avoids the need to convert
into a string what could be binary data. This code block appears in
both the $update and $create functions of crud.cfm, so you'd want to
change it in both places.


On Jan 26, 12:22 pm, Per Djurner <per.djur...@gmail.com> wrote:
> Thanks Daniel, let us know if you find a solution for it and we'll
> include it in the code for the next release.
> / Per
>
Reply all
Reply to author
Forward
0 new messages