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!

IShiva
IShivaAsked:
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.

PluckaCommented:
IShiva,

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.

Regards
Plucka
0
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.

Thanks!

PlayerImageAdd.cfm

<tr>
<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>
<tr valign="baseline">
<td nowrap align="right">&nbsp;</td>
<td><input type="submit" value="Add Image"></td>
</tr>
</table>
<input type="hidden" name="MM_InsertRecord" value="form1">
</cfform>
</td>
</tr>

PlayerImageAddAction.cfm

<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#">
'#UploadedFile#'
<cfelse>
 ''
</cfif>
WHERE PlayerID=#PlayerID#
</cfquery>

<cflocation url="PlayerEditForm.cfm?PlayerID=#PlayerID#">
0
PluckaCommented:
IShiva,

<cftry>
    <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>
    </cfcatch>
</cftry>

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


Regards
Plucka
0

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

Thanks

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

Worked great

IShiva
0
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.