saving blob picture into database without storing to hd - coldfusion

I have an base64 picture string in coldfusion.
How can I save this image blob into the database without storing to filedisk?
My function.
<cfset base64string="base64picturestring">
<cfimage source="#ImageReadBase64("data:image/png;base64,#base64string#")#"
destination="c:\picture.png" action="write" overwrite="true">
<cffile action="readbinary" file="c:\picture.png" variable="ImageData"/>
INSERT INTO imagedb (imageblob)
VALUES (<cfqueryparam cfsqltype="cf_sql_blob" value="#ImageData#" />)
But I don't want to save image on the hard drive.
I need this.
base64----imageblob----database
Any help?

You could use the in-memory filesystem:
http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSe9cbe5cf462523a0-70e2363b121825b20e7-8000.html#WSe9cbe5cf462523a0-540771bc12182683461-8000
<cfset base64string="base64picturestring">
<cfimage source="#ImageReadBase64("data:image/png;base64,#base64string#")#"
destination="ram://src/picture.png" action="write" overwrite="true">
<cffile action="readbinary" file="ram://src/picture.png" variable="ImageData"/>
<cffile action="delete" source = "ram://src/picture.png">
INSERT INTO imagedb (imageblob)
VALUES (<cfqueryparam cfsqltype="cf_sql_blob" value="#ImageData#" />)

ToBinary('#yourString#')
Calculates the binary representation of Base64-encoded data

Related

How to convert base64 to image in coldfusion?

I am trying to convert a base64 string into an image in ColdFusion, but it is not working. From what I have read, this can be done with the ImageReadBase64 function:
<cfset myImage = ImageReadBase64("/9j/4AAQSkZJRgABAQA..............")>
So I tried retrieving a base64 image string from my database:
<cfquery name="GetSignImage" datasource="#application.ds#">
select SIGNIMGBINARY
from T_APPT_sign
where CHECKINID ='#CHECKINID#'
</cfquery>
<cfif GetSignImage.SIGNIMGBINARY neq "">
<cfimage source="#ImageReadBase64(GetSignImage.SIGNIMGBINARY)#" name="signImage" action="resize" width="65%" height="55%">
<cfimage source="#signImage#" action="writeToBrowser">
</cfif>
But I get this error:
The Base64 data is not in proper format. Data should be in the format understood by the <img> tag in HTML, which is "data:image/jpg;base64,[base64 data]"
Can anyone explain what I am doing wrong?

Appending File in Coldfusion

I am taking a Word RTF file and then replacing data within in and then writing it to a specific folder and appending the requestID to the file name. That part works great. However, my client also want to be able to print each word document that is generated at the smae time if there are multiple records.
For this I am reading the same RTF file, replaceing the data within it and then writing to a seperate file that I am attempting to append too within the loop, however it isn't working. I am only getting one record of the query.
<cfloop query="checklistData">
<cffile action="read" file="#request.filePath#\_reports\fileChecklist.rtf" variable="rtf">
<cfset rtf = Replace(rtf,"%PROGRAM%",#rqst_activity#)>
<cfset rtf = Replace(rtf,"%DLA%",#DLA#)>
<cfset rtf = Replace(rtf,"%SPS%",#SPS#)>
<cfset rtf = Replace(rtf,"%ECAT%",#ECAT#)>
<cfset rtf = Replace(rtf,"%MILSTRIP%",#MILSTRIP#)>
<cfset rtf = Replace(rtf,"%TDPNUM%",#tdp_nbr#)>
<cfset rtf = Replace(rtf,"%DTRQST%",#DateFormat(dt_tdp_rcvd,'M/DD/YYYY')#)>
<cfset rtf = Replace(rtf,"%DTCMPT%",#DateFormat(dt_tdp_cmplt,'M/DD/YYYY')#)>
<cfset rtf = Replace(rtf,"%DTDEL%",#DateFormat(rqsted_delivery_dt,'M/DD/YYYY')#)>
<cfset rtf = Replace(rtf,"%UIC%",#DoDAAC#)>
<cfset rtf = Replace(rtf,"%COMMAND%",#COMMAND#)>
<cfset rtf = Replace(rtf,"%DTPRINT%",#DateFormat(NOW(),'M/DD/YYYY')#)>
<cfset rtf = Replace(rtf,"%NSN%",#nsn_id_rqsted#)>
<cfset rtf = Replace(rtf,"%NOMEN%",#nsn_nomenclature#)>
<cfset directoryCheck = "#request.filePath#\requests\documents\#checklistData.id_rqst#">
<cfif directoryExists(directoryCheck)>
<cffile action="write" file="#request.filePath#\requests\documents\#checklistData.id_rqst#\fileChecklist_#checklistData.id_rqst#.doc" output="#rtf#" nameconflict="overwrite">
<cfelse>
<cfdirectory action="create" directory="#directoryCheck#">
<cffile action="write" file="#request.filePath#\requests\documents\#checklistData.id_rqst#\fileChecklist_#checklistData.id_rqst#.doc" output="#rtf#" nameconflict="overwrite">
</cfif>
<cffile action="append" file="#request.filePath#\requests\documents\fileChecklistGroup_#ARGUMENTS.groupID#.rtf" output="#rtf#">
</cfloop>
<cfif NOT LEN('checklistData.groupID')>
<cfheader name="content-disposition" value="filename=#request.filePath#\requests\documents\#requestID#\fileChecklist_#requestID#.doc" />
<cfcontent type="application/msword" file="#request.filePath#\requests\documents\#requestID#\fileChecklist_#requestID#.doc">
<cfelse>
<cfheader name="content-disposition" value="filename=#request.filePath#\requests\documents\fileChecklistGroup_#groupID#.rtf" />
<cfcontent type="application/msword" file="#request.filePath#\requests\documents\fileChecklistGroup_#groupID#.rtf">
</cfif>
<cfabort>
And then I am asking to open the file at the end. Thanks in advance for an help!

How to get the temporary path of a file pulled with CFHTTP in Coldfusion?

I'm using Coldfusion8 and need to fetch images from a remote server, which I'm doing like this:
<cfhttp timeout="45" throwonerror="no" url="#variables.testFilePath#" method="get" useragent="Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12" getasbinary="yes" result="variables.objGet">
<cfset variables.objImage = ImageNew(variables.objGet.FileContent)>
I now need to save the image to Amazon S3, but the function I want to use:
<cfset result = s3.putObject(url.b,file.serverFile,file.contentType,'300',form.cacheControl,'30',form.acl,form.storageClass,form.keyName,GetTempDirectory())>
Requires the directory where my generated image can be found in.
Question:
Is there a way to get the directory of an image file pulled with cfhttp and converted to an image using imageNew? Or do I need to save the file to disk first? I also need to resize before storing, so I might not be able to get by without saving to disk first.
Thanks for pointers!
EDIT:
I got it working like this:
<!--- getAsBinary --->
<cfhttp timeout="45"
throwonerror="no"
url="#variables.testFilePath#"
method="get"
useragent="..."
getasbinary="yes"
result="objGet">
<!--- validate --->
<cfif len(variables.testFilePath) EQ 0>
<cfset variables.errorCount = variables.errorCount+1>
<cfset variables.failedLoads = "FILE NOT FOUND" >
<cfelse>
<cfif len(objGet.Filecontent) EQ 0>
<cfset variables.errorCount = variables.errorCount+1>
<cfset variables.failedLoads = "COULD NOT LOAD IMAGE">
<cfelseif NOT listfindnocase(variables.allow, variables.fileExt) >
<cfset variables.errorCount = variables.errorCount+1>
<cfset variables.failedLoads = "WRONG FILE TYPE">
<cfelse>
<cftry>
<cfscript>
objImage = ImageNew(objGet.FileContent);
ImageSetAntialiasing(objImage,"on");
<!--- resize/crop --->
variables.keyName = Session.loginid & "_S_";
</cfscript>
<!--- convert modified image back to binary --->
<cfset variables.filekey = toBase64( objImage )>
<!--- pass to s3.cfc --->
<cfset result = s3.putObject(variables.bucketName, variables.filekey, variables.contentType, variables.httptimeout, variables.cacheControl, variables.cacheDays, variables.acl, variables.storageClass, variables.keyName, variables.imageSrc, "true" )>
<cfcatch>
<cfset variables.errorCount = variables.errorCount+1>
<cfset variables.failedLoads = "NO IMAGE">
</cfcatch>
</cftry>
</cfif>
I need to re-convert the cropped image to binary, because the s3.putobject will otherwise do another cffile action="readBinary" and breaks on trying to construct the image file path (the image is still in temp) right here:
<cffile action="readBinary" file="#arguments.uploadDir##arguments.fileKey#" variable="binaryFileData">
While I can get the temporary file path using this trick and set uploadDir it doesn't help, because CF docs say the path must be either an absolute path starting with drive letter or slash, otherwise the www-root temp directory will be taken.
In my case the temp www-root directory was on C:/ while the temp file CFFile-Servlet was on E:/ and a relative path did not work either (file not found). So as I found no way to re-read the image from the s3.cfc, I'm now converting back to binary before calling S3.cfc. I pass another parameter (1/0) telling s3.cfc, that I'm already sending the binary image and there is no need to re-read it.
Like so:
<!--- if encoded is true, filekey already is the encoded image --->
<cfif arguments.encoded EQ "true">
<!--- already sending binary image --->
<cfset binaryFileData = arguments.fileKey>
<cfelse>
<!--- Default --->
<cffile action="readBinary" file="#arguments.uploadDir##arguments.fileKey#" variable="binaryFileData">
</cfif>
I'm not sure if this is the smartest way performance wise, but it seems to work pretty smooth. Comments are welcome!
I guess you could use path and file attributes instead of result. Generate some temporary path using GetTempDirectory() + CreateUUID(), fetch and then drop it. Plus it may be a bit more memory-efficient thatn fetching content to the variable, then writing to the intermediate file.
Cfhttp result stores the data in a memory variable.
ImageNew creates a 'ColdFusion' image meaning it's resident in memory only also. You'd have to save it to make it a physical file to send either in cfhttp or imagewrite, etc.
Without saving it to a physical file you must use cffile action = "writetobrowser" to send it to a browser but that ends up saving it in a temp location for the browser to access but wouldn't do you much good here I don't think.
http://livedocs.adobe.com/coldfusion/8/htmldocs/functions_h-im_34.html
http://livedocs.adobe.com/coldfusion/8/htmldocs/Images_19.html
AmazonS3Client has a putObject method that takes an InputStream, so you can wrap the binary data in a ByteArrayInputStream and pass it in without worrying about the backing file.
Something like this:
<cfhttp method="get" url="#local.url#" result="local.httpResult" getAsBinary="true"/>
<cfif local.httpResult.statusCode eq "200 OK">
<cfset var fileInputStream = createObject("java", "java.io.ByteArrayInputStream" ).init(local.httpResult.fileContent) />
<cfset var objectMetadata = createObject("java", "com.amazonaws.services.s3.model.ObjectMetadata" ).init() />
<cfset application.S3UTIL.s3Client.putObject("some.bucket", "folder/file.ext", local.fileInputStream, local.objectMetadata)/>
</cfif>

How to access the object created by a cffile upload in coldfusion

I have an assortment of issues, but I'm going to concentrate on one here. How to I access the object created from a cffile upload. I am currently doing as so.
<cffile action="upload" destination="#Application.filePath#Pics/" filefield="image1" nameconflict="makeunique">
<cfif isDefined ("cffile.serverFile")>
<cfset image1Place = #cffile.serverFile#>
</cfif>
but that doesn't seem like it would work well with multiple file uploads, which happens to be my case.
If you're worried about the result object being blown away as a consequence of multiple invocations of cffile, then you can use the "result" attribute to distinguish them:
<cfset uploadResults = {}>
<cfloop list="#form.filelist#" index="myFile">
<cffile action="upload" destination="#Application.filePath#Pics/"
filefield="#myFile#" nameconflict="makeunique"
result='uploadResults.#myFile#'>
<cfif StructKeyExists(uploadResults, myFile)>
<cfset image1Place = uploadResults[myFile].serverFile>
</cfif>
</cfloop>

How to get file extension when renaming file with cffile

I'm using a cffile tag to upload my file and resave it with a new name. My issue is that the file could be a few different formats and I don't know how to detect the file extension. I'm using the code below:
<cfset ui = createUUID()>
<cffile
action="upload"
accept="video/x-flv, video/mp4, video/x-msvideo"
destination="e:\www2\uploads\#ui#.#cffile.ServerFileExt#"
nameconflict="makeunique"
filefield="form.file"
>
It's telling me that cffile is undefined.
I recommend uploading first, then renaming:
<cfset ui = createUUID()>
<cffile
action="upload"
accept="video/x-flv, video/mp4, video/x-msvideo"
destination="e:\www2\uploads\"
nameconflict="makeunique"
filefield="form.file"
/>
<cffile
action="rename"
source="e:\www2\uploads\#cffile.serverFileName#"
destination="e:\www2\uploads\#ui#.#cffile.serverFileExt#"
/>
I found this awesome function created by Ryan Stille
It should do everything you need
I used it to get the extension then I just created a file name with a UUID
<cffile action="upload" destination="file://upload_#createUUID()#.#fileExt#" nameconflict="makeunique" result="#formField#">