Skip to main content
Inspiring
March 12, 2009
Question

upload mime type errors

  • March 12, 2009
  • 1 reply
  • 436 views
I am uploading images and using the accept attribute in the cffile tag and all works fine except when a user tries to upload an unaccepted file type.
Here is what I have:
<cfif structkeyexists(form,"imagefile")>
<cffile action="UPLOAD" filefield="imagefile" destination="#ExpandPath("uploads")#" nameconflict="MAKEUNIQUE" accept="image/jpg, image/jpeg, image/pjpeg, image/gif, image/tiff" >
</cfif>

How or what is the best way to handle a situation when an unacceptable format is attempted to be uploaded? Mainly the CF error that ensues is what I want to fix. The users would have no idea what that error message means.
    This topic has been closed for replies.

    1 reply

    Inspiring
    March 12, 2009
    Without knowing other aspects of your code for this functionality, I might suggest wrapping the CFFILE tag in a CFTRY/CFCATCH block. You can then catch the error when it occurs and present a more user-friendly message, etc.

    <cfif StructKeyExists(form, "imagefile")>
    <cftry>
    <cffile action="UPLOAD" filefield="imagefile" destination="#ExpandPath("uploads")#" nameconflict="MAKEUNIQUE" accept="image/jpg, image/jpeg, image/pjpeg, image/gif, image/tiff" >
    <cfcatch type="any">
    <!---
    custom error handling here: this could also be a custom type of error to catch
    check CF Docs for more on throwing custom errors
    --->
    </cfcatch>
    </cftry>
    </cffile>