Uploading Files with cf_sebForm

In my ongoing series about using cf_sebForm, I have so far left out one important feature - the ability to upload files. This is essential functionality whether you are saving a record or sending email.

Fortunately, this is easy to do in cf_sebForm, just add a cf_sebField tag of type="file" and indicate the location in which the file should be placed.

For example, if I wanted to upload a file to the "/files/" folder on my site, I could use the following code:

<cf_sebForm>
   <cf_sebField type="file" name="MyFile" destination="#ExpandPath('/files/')#">
   <cf_sebField type="submit" label="Submit">
</cf_sebForm>

Note that I didn't have to specify an "enctype" on the form as cf_sebForm automatically detects the presence of a file type and sets that attribute appropriately.

If I was using this form with a CFC, then cf_sebForm would send a "MyFile" (the field name) argument with a value equal to the name of the file as it was saved on the server (but not including the path information). If I was using this form to send an email, the file would be included as an attachment on the email.

I could also include any of the following attributes for <cffile action="upload">:

  • destination
  • nameconflict (defaults to "Error")
  • mode
  • accept

In addition to using the "accept" attribute to limit the file types on the server side using the Mime-Type of the file (which will return a user-friendly error to the user on the form), I can also use the extensions attribute to create JavaScript validation on the extension of the file.

To limit the file to a PDF, for example:

<cf_sebForm>
   <cf_sebField type="file" name="MyFile" destination="#ExpandPath('/files/')#" accept="application/pdf" extensions="pdf">
   <cf_sebField type="submit" label="Submit">
</cf_sebForm>

If I want to allow the user to upload any image type, I can simply use the type of "image":

<cf_sebForm>
   <cf_sebField type="image" name="MyFile" destination="#ExpandPath('/files/')#">
   <cf_sebField type="submit" label="Submit">
</cf_sebForm>

If I want the user to see the image (or have a link to another file type) when editing a record, I can use the urlpath attribute:

<cf_sebForm>
   <cf_sebField type="image" name="MyFile" destination="#ExpandPath('/files/')#" urlpath="/files/">
   <cf_sebField type="submit" label="Submit">
</cf_sebForm>

That is all it takes to upload a file with cf_sebForm. The syntax I use is actually a bit different, but that involves some other features that I have discussed as well as a few I haven't yet.

The cf_sebForm (tag docs) custom tag is part of the sebtags custom tag set which is open source and free for any use.

Related Blog Entries

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.