Using ColdFusion to save an uploaded image to the server - image

I'm being stymied by what I thought would be the relatively simple task of creating functionality to allow users to upload a picture to a server directory and am looking for some help.
I have a simple form with a straightforward interface, passing a variable named 'imagedata' which is the data passed from a file input, a variable named 'name' which is a specified image name, and a variable named 'imagetype' which passes a file extension.
The code I'm using in the function (handler) to save this file is as follows:
<cfset form.imageName = form.name & "." & form.imagetype />
...
<cffile action="write" file="#sitepath#storage/bannerImages/#form.imagename#" output="#form.imageData#" />
This apparently won't work; it will save a file with the correct name, but does not save the file correctly and doesn't render as an image. I would think that this is something relatively simple, and I would think I've overlooked something because I'm not finding anything on google, which is odd. Help with this problem would be appreciated.

Don't use action="write", use action="upload".
Example:
<cffile
action = "upload"
fileField = "ImageData"
destination = "/tmp/uploads"
accept = "image/jpg"
nameConflict = "MakeUnique"
/>
This will place the file in /tmp/uploads (use for example d:/tmp/uploads on Windows) - which is a directory not accessible online.
At this point, you must verify that the file is what it claims to be - an image file of appropriate dimensions and filesize, and not a masked EXE nor embedded ZIP.
Once you are happy the file is safe for hosting on your server, you can then move it to the appropriate directory and set the name to what was chosen.
Something like...
<cfif cffile.FileWasSaved>
<cfset UploadedFileName = cffile.ServerDirectory & '/' & cffile.ServerFile />
<cfif NOT IsImageFile( UploadedFileName )>
<cfthrow message="Uploaded file not an image." />
</cfif>
<!--- INFO: Checks if zip embedded in image. --->
<cftry>
<cfzip action="list" file="#UploadedFileName#" name="tmp"/>
<cfthrow message="Embedded zip files not allowed."/>
<!--- TODO: Verify correct exception type for CF: --->
<cfcatch type="java.util.zip.zipexception"></cfcatch>
</cftry>
<!---
TODO: Validate any necessary business rules
(e.g. image not too large, etc)
--->
<cfelse>
<cfthrow message="File Upload Error"/>
</cfif>
<cfif refind('\W',Form.Name)>
<cfthrow message="Invalid Name specified. Only alphanumerics allowed."/>
</cfif>
<cfif NOT ListFindNoCase('PNG,JPG,GIF',Form.ImageType)>
<cfthrow message="Invalid FileType specified. Must be PNG/JPG/GIF"/></cfif>
</cfif>
<cffile
action = "rename"
source = "#UploadedFileName#"
destination = "#sitepath#storage/bannerImages/#Form.Name#.#Form.ImageType#"
/>
That code is untested, and - as with everything you run on your servers, but especially so with code involving client uploads - make sure you understand what it is doing before using it.
There is plenty of valuable information on Adobe LiveDocs and on the various CF blogs that is worth spending some time digesting.

Related

Error - Element X is Undefined in Session

Getting this Error
ELEMENT CURRLANGUAGE IS UNDEFINED IN SESSION.
Don't understand why. It was working fine until yesterday
<cfif session.currLanguage eq 'English'>
<cfset session.currLanguage = ''>
</cfif>
Because currLanguage is not defined in the session scope. Womp Womp.
Try this to see what IS defined.
<cfdump var="#SESSION#">
It could be that something is not being set when it should. We'd have to see your code to tell for sure. If you think it's working okay and want to prevent unnecessary error messages in the future, you can do a check like this:
<cfif structKeyExists(SESSION, "currLanguage") AND SESSION.currLanguage is "English">
Logic Logic Logic
</cfif>
Please confirm those too
Please check the value update in the session scope properly.
The session scope will be created based on application name. so if we created the application name as dynamic or updated recently then the session scope will be created as new

ColdFusion Proxy Server

I'm trying to create a basic proxy server so I can keep track of what my kids are doing web wise - I know there are products out there but I thought it would be an interesting exercise to write one myself.
I have the following code that kind of works but doesn't pull any images or css through - I guess because it makes another call to the remote server and gets confused
<cfhttp url="https://www.bbc.co.uk">
<cfhttpparam type="header" name="Proxy-Connection" value="keep-alive" >
<cfhttpparam type="header" name="Accept" value="application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5">
<cfhttpparam type="header" name="Accept-Language" value="en-US,en;q=0.8">
<cfhttpparam type="header" name="Accept-Charset" value="ISO-8859-1,utf-8;q=0.7,*;q=0.3">
</cfhttp>
<cfset html = cfhttp.FileContent />
<cfoutput>#html#</cfoutput>`
What am I missing?
What you want to use is the resolveurl parameter of - set it to yes/true. This defaults to no.
What this parameter does is resolves relative paths to absolute paths automatically for you.
Now, if you want to change those paths as well, you would change them to also route through your http proxy script, but there won't be much use as you won't know much about the content regardless.
resolveurl should hook you up with what you are looking for. cheers.
https://cfdocs.org/cfhttp (look for resolveurl tag attribute)

ImageScaleToFit - how to implement?

I'm new to Railo, moving away from ColdFusion 8 where my site used to used to use cfx_imagecr3.
I believe Railo has ImageScaleToFit but I'm not sure if I'm using it correctly or if I need to add some kind of class/component? I've added it between a cfscript but i get 'invalid variable declaration' which doest make any sense to me. Any help appreciated.
I'm running railo-4.0.0.013 on ubuntu 12.04
<cfscript>
imageScaletoFit('/home/images/testimage.jpg','90','114','highestPerformance');
</cfscript>
You need to first load the image into a variable, then use that variable name inside ImageScaletoFit, like so:
<cfimage name="ImgVariable" source="/home/images/testimage.jpg" />
<cfset imageScaletoFit(ImgVariable,'90','114','highestPerformance') />
(This isn't specific to Railo - the function exists and works the same way in CF8+)

Send Email Using Classic ASP with an Embeded Image

i am making NewsLetter using the wysiwyg Editor.. it allows me to upload the Image Path
and Image Path is stored in the Upload Directory..
Not When i retrieve that Image using it works in website..
the editor's value is stored in database
example
<br> hi
<img src="upload/acb.gif">
<br>
Hello
i am sending Email and the detail of this email is received from database
and this detail is sent to visitor
he is gettion all text value but not able to see Image
so suggest me what to do..?
If you are sending emails using CDOSYS.Message, you can easily send a complete web page with embedded images using the Message.CreateMHTMLBody(url) method.
Dim Message
Set Message = CreateObject("CDOSYS.Message")
Message.From = "from#email.org"
Message.To = "to#email.org"
Message.CreateMTHMLBody "http://yourserver.org/email.html"
Message.Send()
I recently cleaned up some code I had lying around to do this and slapped it online as a "Gist" on github; hope it still helps someone!
Sending embedded images with CDOSYS
This solution uses CDO (CDOSYS / CDO.Message), with "AddAttachment", and manually controlling the properties of the attachments to make them usable from within the email HTML and to avoid them appearing as separately-downloadable attachments in an email client.
The usage is very simple, just reference the images by a local path (on the computer the code is running on) in the HTML of the message, eg:
Some Image: <img src="<EMBEDDEDIMAGE:C:\test.jpeg>" />
The code will pick up the filename, add the file as an attachment to the message, and replace the relevant part of the message HTML with the internal reference to that attachment.
You would have to add site url to img source
<img src="http://www.sitename.com/upload/acb.gif"> as the user is not accessing your site from his mailbox.
For this you can set "http://www.sitename.com/" as a key in web.config and use in your mails.
This will resolve your problem for sure. Happy coding !!!!!!!!!!!!!
You would use AddRelatedBodyPart:
Embed Usage
Create Array and Pass it in "SendMail" Function as Parameter
Use in Email Body e.g.
Dim arrRelatedBodyPart(1)
arrRelatedBodyPart(0) = Server.MapPath(".") & "/images/barcode/bar_blk.gif"
arrRelatedBodyPart(1) = Server.MapPath(".") & "/images/barcode/bar_wht.gif"
Example
For i = 0 To UBound(arrRelatedBodyPart)
Dim strPathAndFileName: strPathAndFileName = arrRelatedBodyPart(i)
Dim strFileName: strFileName = GetFileName(arrRelatedBodyPart(i), "/")
'.AddRelatedBodyPart strPathAndFileName, strFileName, cdoRefTypeId
Set objCDOBodyPart = .AddRelatedBodyPart(strPathAndFileName, strFileName, 1)
objCDOBodyPart.Fields.Item("urn:schemas:mailheader:Content-ID") = "<" & strFileName & ">"
objCDOBodyPart.Fields.Update
Next
What are you using to send the email, I have had success in the past using AspEmail: http://www.aspemail.com/
It explains how to send embedded images here: http://www.aspemail.com/manual_04.html
However you will have to get it installed on your server, if you are using Shared hosting this might be a problem, if you are running your own server pretty easy!

Creating an internal clock within Coldfusion .cfc file to execute scheduled tasks

I am needing to do scheduled tasks based off an ever going internal clock. I know this is possible with Javascript, but this solution will need to be hosted within a Application.cfc file. I tried to just use a "Now" function within a OnRequestStart function, however the problem is if I need to let's say send an email at midnight to someone, but no one makes a request at midnight, then the email won't send. I tried to do the same function as well in the OnApplicationStart function, but that will only run once ideally. I was thinking about using the CreateTime method within the OnApplicationStart function, but how would I increment the time in real time, if that makes sense. I hope this is more clear on what is actually needed. Here is some code I tried out, however I am not really sure how to test if this works honestly.
<cffunction NAME="internalclock">
<cfset today = #day(Now())#>
<cfset h = #hour(Now())#>
<cfset m = #minute(Now())#>
<cfset s = #second(Now())#>
<cfset m = #checktime(m)#>
<cfset s = #checktime(s)#>
<cfsetting requestTimeOut = ".5">
</cffunction>
<cffunction NAME="checktime">
<cfif i LESS THAN 10 > i="0"+i
<cfreturn i>
</cfif>
</cffunction>
Edit:
I'm having some issues accessing a variable within a page2.cfc file. The way the data is flowing is that I initialize the variable in an Application.cfc file, rewrite the same variable in page2.cfc, and access the modified variable in page3.cfm. Page3.cfm is just a test to ensure that the variable is in fact defined and updating as expected. I should mention that page2.cfc has a scheduled task on it and runs around every minute. Here is some example code:
Application.cfc
<CFFUNCTION NAME="OnApplicationStart">
<cfset Application.currenttime = 0>
</CFFUNCTION>
page2.cfc
<CFFUNCTION NAME ="Counter">
<cfset Application.currenttime = #Now()#>
</CFFUNCTION>
page3.cfm
<cfoutput>
#Application.currenttime#
</cfoutput>
The error I am getting is that either the current time variable is displaying as 0, or if I change the output to Counter.currenttime it says it's not defined. I am sure I am doing something dumb, but I would appreciate the help.
Thank you all for your help.
This can actually be done via Scheduled Tasks within Coldfusion Administrator. I didn't know that was a thing before actually making this post. My apologies.
Thank you,

Resources