Coldfusion upload and rename a file.

Shock; my second post in a week (most likely NOT a regular occurrence). Today I solved a problem that, to me, seems difficult to find a solution online. This is the issue (this may only be specific to railo now) that if you perform the following code, (assuming a file submitted from a form) the file is uploaded but errors with “cannot move file” on the rename. I use it slightly modified to keep versions of files.

<cffile action="upload" fileField="file" destination="#ExpandPath("./")#" nameConflict="makeUnique" result="upload">
<cffile action="rename" source="#ExpandPath("./")#" destination="#ExpandPath("/document_#otherversions + 1#.#upload.serverFileExt#")#">

My solution is simple, I use the java File object to rename it, apparently this fixes the issue. As follows:

<cffile action="upload" fileField="file" destination="#ExpandPath("./")#" nameConflict="makeUnique" result="upload">
<cfdirectory directory="#ExpandPath(filedirectory)#" action="list" filter="documents_*" name="historic" sort="desc">
<cfset javaFile = CreateObject("java", "java.io.File").init(ExpandPath("./" & upload.serverfile))>
<cfset renamePass = javaFile.renameTo(CreateObject("java", "java.io.File").init(ExpandPath("/document_#otherversions + 1#.#upload.serverFileExt#")))>

About Simeon Cheeseman

I enjoy a wide variety of computer and board games, have a BSc in Computer Science and have played percussion for 18 years.

Posted on February 23, 2012, in ColdFusion, Railo and tagged , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: