• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

ByteArray objects cannot be converted to strings

Community Beginner ,
Sep 01, 2011 Sep 01, 2011

Copy link to clipboard

Copied

I am trying to manage images using cf and mysql. What a pain! My SQL doesn't play nice with images so I'm finding out.

Here is what I have done. I have blobs enabled in mysol admin. Now that I did that, I can't just read the image values I uploaded to the server, I have to read a byteArray. How do I do this??

This is the code giving me a problem:

<cfif projMan.thumb is true>

#toString(thumb)#

</cfif>

My cfif is throwing this error.

I also need it to be read if I update a record.. this is my insert update code:

<cffile Accept="image/*" action = "upload" Destination="D:\wwwroot\mysite\images\contentfile\" fileField = "MYFile" nameConflict = "overwrite">
<cfset uploadedfile = cffile.serverfile>

<cfif form.id EQ 0>
<cfquery name="InsertDetails" datasource="#sitedatasource#">
    INSERT INTO recipes
    (title, Body, thumb, descript, categoryID)
    VALUES
(<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.title#">,
<cfqueryparam cfsqltype="cf_sql_longvarchar" value="#form.Body#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#uploadedfile#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.descript#">,
<cfqueryparam value="#form.categoryID#" cfsqlType="CF_SQL_INTEGER">)
</cfquery>

<cfelse>

<cfquery name="UpdateDetails" datasource="#sitedatasource#">
UPDATE recipes
SET
recipes.title=<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.title#">,
recipes.Body=<cfqueryparam cfsqltype="cf_sql_longvarchar" value="#form.Body#">,
<cfif uploadedfile is true>
recipes.thumb=<cfqueryparam cfsqltype="cf_sql_varchar" value="#uploadedfile#">,
</cfif>
recipes.descript=<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.descript#">,
recipes.CategoryID=<cfqueryparam value="#form.categoryID#" cfsqlType="CF_SQL_INTEGER">
WHERE recID = <cfqueryparam value="#form.ID#" cfsqlType="CF_SQL_INTEGER">
</cfquery>

</cfif>

Can anyone help me out? I'm hoping this is a simple solution.. I have been at this for 2 days now, and this is my last issue. It is also my first time using mysql for image management.

Thank you.

TOPICS
Advanced techniques

Views

3.6K

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
Valorous Hero ,
Sep 01, 2011 Sep 01, 2011

Copy link to clipboard

Copied

Here is what I have done. I have blobs enabled in mysol

admin. Now that I did that, I can't just read the image

values I uploaded to the server, I have to read a byteArray.

How do I do this??

The description of the problem and the code are giving conflicting stories. What is it you want to store in your database - the actual content of the images or just the file names (ie "myImage.png")?

Can you post the full error message?

Votes

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
Community Beginner ,
Sep 01, 2011 Sep 01, 2011

Copy link to clipboard

Copied

yes, just the myImage.png is to go into the db. There is no error, it just won't write the file name to the DB for me to access it later. Driving me crazy, SQL is easier than this.. I don't get where the problem is.

I fixed the ByteArray issue, but now I can't get it to write to the db.

Votes

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
Valorous Hero ,
Sep 01, 2011 Sep 01, 2011

Copy link to clipboard

Copied

There is no error, it just won't write the file name to

the DB for me to access it later.

You need to be more specific. First, is the problem with your UPDATE or INSERT query? Second what do you mean by "won't write the file name"? Is the file name being erased, not updated, or something else ...?

Votes

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
Community Beginner ,
Sep 01, 2011 Sep 01, 2011

Copy link to clipboard

Copied

it won't insert it or update it. BUT it does load it to the server, just not to the db. There are no errors, just no image name in the cell. It's just not writting it to the DB at all. it's a mysql db and blobs are enabled in this cell for image data.

Votes

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
Valorous Hero ,
Sep 01, 2011 Sep 01, 2011

Copy link to clipboard

Copied

So reading between the lines here, when you say "cell" do you mean a column in your database table? Are you saying you set the data type of your "Thumb" column to BLOB rather than VARCHAR?

Unless I am missing something, you do not need to use blobs.  You are  only storing file names - simple strings that can be stored in a varchar column.  You only need to use the BLOB data type if you are intending to store the actual content of the image (ie binary data) in your database.

Message was edited by: -==cfSearching==-

Votes

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
Community Beginner ,
Sep 02, 2011 Sep 02, 2011

Copy link to clipboard

Copied

sorry, I had to run out yesturday

I only went to blob because I couldn't get it to work with VARCHAR. I had the same issue, and right now, my code is the one I was using as a VARCHAR don't understand why it won't work that way either. It works all the time for me in SQL and even access.

Yes, I meant column, and I'm going to turn off blob, go back to the norm, and I'm looking to just put the filename into the db.

Votes

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
Valorous Hero ,
Sep 02, 2011 Sep 02, 2011

Copy link to clipboard

Copied

Well like I said BLOB is the wrong data type if you are just storing file names. You should be using VARCHAR instead.

<cfif uploadedfile is true>

   recipes.thumb=<cfqueryparam cfsqltype="cf_sql_varchar" value="#uploadedfile#">,
</cfif>

I am a bit skeptical about your INSERT not working.  Though I can see problems with your update. If #uploadedfile# is a file name, the expression above will almost never evaluate to true.  But rather than guessing, you need to do some debugging.

1) Dump the #uploadedfile# variable before your queries. What is the value? Maybe it is empty for some reason.

2) Enable debugging in the CF Admin. What is the generated SQL being sent to your database?

Votes

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
Community Beginner ,
Sep 02, 2011 Sep 02, 2011

Copy link to clipboard

Copied

I did a dump, and the image is there just before the query, so it is in my query there is a problem, that is for the update. The insert seems to be working, BUT I also need it to check if there is a new file for an update, if there is, delete it, and add the new one. I have that logic in there, but it's not working. So my cfif must be wrong saying if uploadedfile is true. How else would I look at this so the update would work.

Also in the update, I have this code:

<cfif uploadedfile is true AND len(trim(form.oldimage))>
<cfif FileExists(pathToFile & form.oldimage)>
<cffile action="delete" file="#pathToFile & form.oldimage#">
</cfif>
</cfif>

then it inserts it with the code above.

So adding a new image / record works, it's now down to the update being a problem

Votes

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
Valorous Hero ,
Sep 02, 2011 Sep 02, 2011

Copy link to clipboard

Copied

I also need it to check if there is a new file for an update, if there is, delete it, and add the new one.

One problem at a time.  That code is probably incorrect, but it should not have any impact on your update query. Get the query working first. Then deal with cleaning up old files.

1) Dump the #uploadedfile# variable before your queries. What is the value?

2) Enable debugging in the CF Admin. What is the generated SQL being sent to your database?

So getting back to the update query ...  what were the results?  I would guess the "Thumb" column was not included in the sql.  But things go faster when we do not have to resort to guessing 😉

Votes

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
Community Beginner ,
Sep 02, 2011 Sep 02, 2011

Copy link to clipboard

Copied

well, I don't have full control of this server. I did have them turn on the debugging, it has a white page on the bottom with a lot of info in it. (CF9) from what I read here, It says that the thumb is in the query.

This is part of what it says in SQL Queries section of this debugger: Time=0ms, Records=2

Is this what you are looking for?

This is the most I can get out of the server, it has cookie variables, debugging info, execution time.. is there info in here you want?

Votes

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
Valorous Hero ,
Sep 02, 2011 Sep 02, 2011

Copy link to clipboard

Copied

Is this what you are looking for?

We need to see the SQL statement from that section yes  (cfqueryparam values may be listed separately).

All you are trying to do is view the SQL and see what the statement is actually doing, since you say the results are not what you expected.

It says that the thumb is in the query.

Not what I would have expected. But that is about all I can say without seeing the generated sql.

Votes

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
Community Beginner ,
Sep 02, 2011 Sep 02, 2011

Copy link to clipboard

Copied

how do I get the generated sql? Is it possible that my statemant
<cfif uploadedfile is true> is wrong? I do a dump, and the image name is in the dump, it also adds it to the insert new record. I do a dump just before theupdate and the image is in the dump.

So it has to be my code for the update. Should I make it more like this:


<cfif len(trim(form.MYFile))>
<cftry>
<cffile Accept="image/*" action = "upload" Destination="D:\wwwroot\chefandserve_com\images\contentfile\" fileField = "MYFile" nameConflict = "makeunique">

<cfset fileuploaded = true>
<cfset uploadedfile = cffile.serverfile>
<cfcatch type="any">
<!--- if upload did not suceed, reset file uploading vars --->
<cfset fileuploaded = false>
<cfset uploadedfile = "">
<!--- this can be further enhanced by setting some var to hold error
message and return it to user --->
</cfcatch>
</cftry>
</cfif>

right now, this isn't working, but it is part of what I was missing for the

<cfif uploadedfile is true>

Is there any way we can fix this and get it working????

Votes

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
Valorous Hero ,
Sep 02, 2011 Sep 02, 2011

Copy link to clipboard

Copied

how do I get the generated sql?

IF the proper settings were enabled, the SQL should be directly beneath the execution times you posted. But the update statement should be pretty obvious. Are you saying you do not see any sql statements ...  at all?  ( BTW: This forums loves to mangle things. So if you actually did post the sql it did not show up )

Is it possible that my statemant <cfif uploadedfile is true> is wrong?

Yes. Like I said earlier you are treating #uploadedfile# as if it contains a boolean (ie true/false) value. It does not. It contains a string/file name. So the code is actually doing this:

                 <cfif  "myFileA.png" equals  true>

                     .... then update the "thumb" column

                 </cfif>


That expression will never be true, so the "Thumb" column will NOT be updated.  So you either need to remove the CFIF or change the comparison to something more appropriate.

It says that the thumb is in the query.

Either way, it makes me a little skeptical about that comment. I do not see how the "Thumb" column could be included in the UPDATE unless you are using different code than what you posted.

Votes

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
Community Beginner ,
Sep 02, 2011 Sep 02, 2011

Copy link to clipboard

Copied

what if I use an isdefined instead? or even <cfif uploadedfile GT 0>
Would this work, I believe it's the if statement that is making the problems.

I did notice this forum does mangle posts. this is the SQL that it generates in the debug:

SQL Queries

getRecipes (Datasource=mydb, Time=0ms, Records=3) in D:\wwwroot\mydomain\images\admin\recipeManage.cfm @ 20:48:34.03

SELECT recipes.recID AS ID, recipes.title AS recTitle, recipes.Body, recipes.thumb, recipes.descript
FROM recipes
ORDER BY recipes.CategoryID

does this help?

Votes

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
Valorous Hero ,
Sep 06, 2011 Sep 06, 2011

Copy link to clipboard

Copied

LATEST

what if I use an isdefined instead? or even <cfif

uploadedfile GT 0>

Would this work, I believe it's the if statement that is

making the problems.

It all depends on what you are doing when the user does not upload a new file. We need to see the full code, not just snippets.

Votes

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
Resources
Documentation