Aug 8, 2018

Get the file name of an uploaded file in ColdFusion

The file gets stored in a tmp folder before it is moved to its intended location. I know a lot of people that wanted to run validations on the file before this happens. Writing a cfdump of the form post will reveal the file but it will look like this :

The original file name is not included in the form post, one of the workarounds I have seen was to use Javascript to populate a hidden input field with the file name, this hidden field would then be included in the form structure. We know that Coldfusion knows the file name as it uses it to rename the tmp file to the file name once the file is moved to its intended location.

I did find that the file name is included in the form post but it is not obvious on how to get to it.

/* BEGIN - get uploaded image file name */
uploadedFileName = "";
getPartsArray = form.getPartsArray();
arrayIndex = arrayFind(getPartsArray,
{if(left(s, 38) == "com.oreilly.servlet.multipart.FilePart" )
return true; return false;});
if ( arrayIndex GT 0 ) {
uploadedFileName = getPartsArray[arrayIndex].getFileName();
/* END - get uploaded image file name */

I found the array being returned was not always consistent, hence the need to search through the array for the desired object. There probably is a better way to access this, and I will look into that when I have more time.

No comments: