Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload File to Database in ColdFusion

I simply would like to upload a file to my database using ColdFusion. I understand how to upload an image to a directory, but I would like to place it directly in the database.

I have set a database field to varbinary(MAX) to accept the image and have the stored procedure to insert it. Currently my code for uploading the image to my file system is:

<cfif isdefined("form.FileUploadImage")> 
            <cffile action="upload" filefield="FileUploadImage" destination="#uploadfolder#" nameconflict="overwrite" accept="image/*" > 
</cfif>

I've obviously left some of the supporting code out, but really all I need to do is get a binary representation of the file stored in memory, instead of the file system.

Any experts out there that can help?

Thanks, George

like image 781
George Johnston Avatar asked Dec 18 '22 02:12

George Johnston


2 Answers

Something like this?

<cfquery>
  INSERT INTO Image (Jpg) 
  VALUES (
    <cfqueryparam CFSQLType="CF_SQL_BLOB"
                  value="#ToBase64(FileReadBinary(uploadedFilePath))#">
</cfquery>

Later if you want to stream the image back to the browser, use <cfimage> (CF8+)

like image 106
Henry Avatar answered Dec 19 '22 15:12

Henry


The way to bypass saving file to the file system:

<cfqueryparam cfsqltype="cf_sql_blob"
              value="#FileReadBinary(FORM.FileUploadImage)#">
like image 26
Roman Avatar answered Dec 19 '22 15:12

Roman