Copy link to clipboard
Copied
Hi i am trying to create a page that when a user uploads an image it will check that it is an image file and then print the image name and put that name into the database.
I have got the following code but it only works if the user selects an image file. If they select any other file type i want it to give an error message but i can not get this to work. Any suggestions
<CFIF isdefined ("url.Action")> <!--- Test the file size --->
<CFIF val(cgi.content_length) gt 1024000> <!--- the file size is over the limit of 1mb. Refuse to upload it --->
<CFSET variables.Success="Im sorry. The file size is over 1mb. Please select a smaller image">
<CFELSE>
<!--- the file is within the limit specified. Define the accepted MIME types. this example accepts plain text files and zip files. --->
<CFSET request.AcceptImage= "image/gif,image/jpg,image/jpeg,image/pjpeg,image/x-png"><!--- now try to upload the file --->
<CFTRY>
<cfset FileToDir = "C:">
<!--- upload the --->
<cffile action="upload" filefield="FileContents" destination="#FileToDir#" nameconflict="Overwrite" accept="#request.AcceptImage#">
<CFSET variables.Success="Uploaded.">
<CFCATCH type="Application"><!--- something went wrong. Was it a mime type failure? --->
<CFIF isdefined("cfcatch.MimeType")> <!--- yes it was. show the friendly error message. --->
<CFIF not ListContains (request.AcceptImage,cfcatch.MimeType)><H1>Im Sorry</H1>This type of file is not allowed for upload.<BR>All that user training really paid off.</P>
<P>Try again...</P>
<CFELSE>
<!--- Hmmm. the mimetype is there but the file was on the list. Better dump out the whole error message. --->
<CFOUTPUT>
<B>Error</B><BR>#cfcatch.Message# #cfcatch.Detail#
</CFOUTPUT>
</CFIF>
<CFELSE>
<!--- Hmmm. No mimetype error in the catch scope. Better dump out the whole error message. --->
<CFOUTPUT>
<B>Error</B><BR>#cfcatch.Message# #cfcatch.Detail#
</CFOUTPUT>
</CFIF>
<CFABORT></CFCATCH>
<CFCATCH type="Any">
<CFOUTPUT>
<B>Error</B><BR>#cfcatch.Message# #cfcatch.Detail#
</CFOUTPUT>
<CFABORT></CFCATCH></CFTRY>
</CFIF>
</CFIF>
<!--- Display the form --->
<HTML>
<HEAD>
<TITLE>Uploader Test</TITLE>
</HEAD>
<BODY>
<CFIF isdefined ("url.Action")>
<cfset myFileName = "#CFFILE.ServerFile#">
<CFOUTPUT>
Your file <strong>#myFileName#</strong> #variables.Success#
</CFOUTPUT>
</CFIF>
<CFOUTPUT>
<FORM action="#cgi.script_name#?Action=Y" method="post" enctype="multipart/form-data">
</CFOUTPUT>
Source File Name:<BR>
<INPUT name="FileContents" type="FILE" size="45"><BR>
<INPUT type="submit" value="Upload File">
</FORM>
Copy link to clipboard
Copied
How is this not working?
What is the behavior, what is the desired behavior how are they different?
What about using the isImage(), isImageFile(), imageInfo() or other image functions?
http://livedocs.adobe.com/coldfusion/8/htmldocs/functions-pt0_11.html#1099325