Show an error message when user tries to upload file other than jpg or gif

I have users uploading images to a site. I have it set up so that only jpg or gif files can be uploaded (in the cffile tag). If someone tries to upload a different file format, the CF error message shows. How can I show my own error message when this happens? I currently have an ImageUpload.cfm page and and ImageUploadAction.cfm page.

Thanks much!

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Use a try catch block around your statements and catch the error.

Or allow all types of files, and then look to see what it is after the fact.

IShivaAuthor Commented:
Thanks for the reply Plucka.

Can you provide some code for this? I have never used cftry or cfcatch before. Here is part of the code for the 2 pages I mentioned earlier.



<td class="frame">
<cfform method="post" name="form1" enctype="multipart/form-data" action="PlayerImageAddAction.cfm?PlayerID=#URL.PlayerID#">
<table align="left">

<tr valign="baseline">
<td nowrap align="right">Player Image:</td>
<td><input type="file" name="PlayerImage" id="ul_path"></td>
<tr valign="baseline">
<td nowrap align="right">&nbsp;</td>
<td><input type="submit" value="Add Image"></td>
<input type="hidden" name="MM_InsertRecord" value="form1">


<cfset PlayerID = #URL.PlayerID#>

<cfquery datasource="newshs">
UPDATE #SESSION.Auth.tblPlayers# SET PlayerImage=

<cfif IsDefined("FORM.PlayerImage") AND #FORM.PlayerImage# NEQ "">
<cffile action="upload" filefield="PlayerImage" destination= "c:\inetpub\wwwroot\shacom\#Session.Auth.hsSport#\pictures\players" accept="image/jpeg, image/pjpeg, image/gif" nameconflict="makeunique">
<cfset UploadedFile = "#file.serverfile#">
WHERE PlayerID=#PlayerID#

<cflocation url="PlayerEditForm.cfm?PlayerID=#PlayerID#">

    <cffile action="upload" filefield="PlayerImage" destination= "c:\inetpub\wwwroot\shacom\#Session.Auth.hsSport#\pictures\players" accept="image/jpeg,image/pjpeg, image/gif" nameconflict="makeunique" />

    <cfcatch type="any">
        <cfoutput>Sorry could not upload this file type</cfoutput>

They type = "any" will catch any type of error occuring within the try block.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IShivaAuthor Commented:
Sorry for the delay in responding.

I will give that a shot


IShivaAuthor Commented:
Well, that was pretty much pain free! Thanks a lot plucka!

Worked great

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.