Uploading Files with cf_sebForm II

In my last entry on handing files with cf_sebForm, I ended the entry by saying that the method I had just covered for handling files with cf_sebForm wasn't the one that I actually used. It was close, but a few differences save me some typing.

Before getting into my approach, I little background is in order. I have one central location for file uploads for any given site. Each file upload function gets a different subfolder. For example, I might upload all files to the "/files/" folder. Then resumes might go in the "/files/resumes/" folder and avatars might go in the "/files/avatars/" folder. If your approach doesn't match this one, then the following technique will be of little use. If it does, read on.

Given that all of my files are going to the same place, I set two attributes in cf_sebForm to indicate that location:

<cf_sebForm UploadBrowserPath="/files/" UploadFilePath="#ExpandPath('/files/')#">
...
</cf_sebForm>

Now cf_sebForm know where I put all of my files. I don't have cf_sebForm automatically determine the UploadFilePath, because the sebForm.cfm might not be stored in the site's directory.

So, the example that I used in my previous entry would now look like this:

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

The file from the "MyFile" field would now be uploaded to "/files/myfiles/". At first glance, this may not seem to have gained me anything. In fact, if I am only uploading files with one field on one form, then I haven't.

If, however, I am uploading multiple files on this form then I have. More significantly, if I am uploading files on multiple forms then I can save myself a lot of code (and centralize my settings).

Remember, with cf_sebForm, I can use request variables to set default values for any attributes.

So, I can include the following code in Application.cfm/cfc:

<cfscript>
request.cftags = StructNew();
request.cftags.sebtags = StructNew();
request.cftags.cf_sebForm = StructNew();
request.cftags.cf_sebForm.UploadBrowserPath = "/files/";
request.cftags.cf_sebForm.UploadFilePath = ExpandPath("/files/");
</cfscript>

Having done that, my form can look like this:

<cf_sebForm>
<cf_sebField type="file" name="MyFile" folder="myfiles">
<cf_sebField type="submit" label="Submit">
</cf_sebForm>

Again, if I have only one form then this isn't worthwhile. If, however, I may have multiple forms uploading files then this make for some tight, clean code and centralize my settings.

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.