Adobe ColdFusion 8

Controlling the type of file uploaded

For some applications, you might want to restrict the type of file that is uploaded. For example, you might not want to accept graphic files in a document library.

You use the accept attribute to restrict the type of file that you allow in an upload. When an accept qualifier is present, the uploaded file's MIME content type must match the criteria specified or an error occurs. The accept attribute takes a comma-separated list of MIME data names, optionally with wildcards.

A file's MIME type is determined by the browser. Common types, such as image/gif and text/plain, are registered in the browser.

Note: Current versions of Microsoft Internet Explorer and Netscape support MIME type associations. Other browsers and earlier versions might ignore these associations.

ColdFusion saves any uploaded file if you omit the accept attribute or specify "*/*". You can restrict the file types, as demonstrated in the following examples.

The following cffile tag saves an image file only if it is in the GIF format:

<cffile action="Upload"
    fileField="Form.FiletoUpload"
    destination="c:\uploads\"
    nameConflict="Overwrite"
    accept="image/gif">

The following cffile tag saves an image file only if it is in GIF or JPEG format:

<cffile action="Upload"
    fileField="Form.FiletoUpload"
    destination="c:\uploads\"
    nameConflict="Overwrite"
    accept="image/gif, image/jpeg"> 

Note: If you receive an error similar to "The MIME type of the uploaded file (image/jpeg) was not accepted by the server", enter accept="image/jpeg" to accept JPEG files.

This cffile tag saves any image file, regardless of the format:

<cffile action="Upload"
    fileField="Form.FiletoUpload"
    destination="c:\uploads\"
    nameConflict="Overwrite"
    accept="image/*">