Skip to main content
Participant
February 22, 2010
Question

cffile upload using accept= image/*

  • February 22, 2010
  • 1 reply
  • 2732 views

I am running a site that allows users to upload their picture to a profile.  I want to ensure that people only upload a picture format: jpg, gif, png...  So i have this code

<cf_sanfile

action="upload" filefield="upload" nameconflict="overwrite" destination="#File_Path#\member\profile\photo" enctype="multipart/form-data" accept="image/*">

Works great but is there a way to redirect them to a new page or use an alert box to notify them when it is not a picture format instead of the site just referring them to the site admin?

    This topic has been closed for replies.

    1 reply

    BKBK
    Community Expert
    Community Expert
    February 22, 2010

    <cfif isDefined("Form.FileContents") >
        <cftry>  
        <cffile action = "upload"
            fileField = "FileContents"
            destination = "c:\uploads"
            accept="image/*"
            nameConflict = "overwrite">

            <cfif cffile.FileWasSaved>Upload done!</cfif>
        <cfcatch type="any">
            <!--- if MIME type doesn't begin with 'image/' --->
            <cfif "image/" is not left(cfcatch.MIMEType,6)>
                <!--- Alternatively, display your custom message --->
                <span style="color:red">Attention:</span> <cfoutput>#cfcatch.message#</cfoutput><br><br>       
             </cfif>  
            <!--- the user-friendly thing to do: take user back to upload form page --->
            <a href="uploadForm.cfm" title="Upload">Upload image</a>       
        </cfcatch> 

         
        </cftry>
      
    <cfelse>
        <form method="post" action="uploadForm.cfm" enctype="multipart/form-data">
            <input name="FileContents" type="file">
            <br>
            <input name="sbmt" type="submit" value="Upload image">
        </form>
    </cfif>