2

I have a requirement to store image blob data in a table. I know storing an image in blob format within a database table is not best practice. But doing so is a client requirement. I am using ColdFusion 10 and SQL Server 2008. I have used this snippet to insert the image blob data in SQL server .

<cfimage action="read" name="imdata" source="C:\inetpub\wwwroot\a.jpg" >

<cfquery datasource="localdsn">
  INSERT INTO imtbl(image)
  VALUES #imageGetBlob(imdata)#
</cfquery>

But it is throwing error

ByteArray objects cannot be converted to strings.

I have also tried using #toString(imageGetBlob(imdata))# Still no success.

I have gone through https://forums.adobe.com/thread/60629 But can not find any solution.

9
  • 2
    Did you enable the BLOB setting on your ColdFusion datasource? Commented Feb 3, 2015 at 14:51
  • 1
    Have you tried parameterising the blog with cfqueryparam? Something like <cfqueryparam cfsqltype="cf_sql_blob" value="#imageGetBlob(imdata)#"> might help expose whats going on. Commented Feb 3, 2015 at 15:25
  • @Miguel-F: Yes I have enabled it , Still not working, Commented Feb 3, 2015 at 15:30
  • 1
    Almost. FileReadBinary() expects a file path. You are passing in an image variable. Get rid of the cfimage and use FileReadBinary("C:\inetpub\wwwroot\a.jpg") inside your cfqueryparam. Also, you obviously need parenthesis in your VALUES (...) clause. Commented Feb 4, 2015 at 13:50
  • 1
    I would add the following notice: If you use the image type in SQL Server (will be removed in a future version), please replace it with varbinary(max) - VarBinary vs Image SQL Server Data Type to Store Binary Data? Commented Jul 10, 2016 at 6:26

1 Answer 1

5

Finally I have fixed the issue. Two things I have done is,

  1. I have enabled BLOB settings for the data source.

  2. I have finally used this query

<cfquery datasource="localdsn"> INSERT INTO imtbl(image) VALUES ( <cfqueryparam cfsqltype="cf_sql_blob" value="#fileReadBinary('C:\inetpub\wwwroot\a.jpg')#"> ) </cfquery>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.