• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 497
  • Last Modified:

Attach file(s) to email generated by ColdFusion in response to an HTML form

I am working with an HTML form that is in turn handled by ColdFusion.  The form asks some basic questions then gives the user the option of attaching up to three (image) files.  the form can be seen here:

http://www.cambridgema.gov/cdd/et/bike/forms/bike_rack_form1.cfm

The form data is handled by:

http://www.cambridgema.gov/cdd/et/bike/forms/bike_rack_sent1.cfm

The problem comes when the form data is handled by ColdFusion.  If one or more of the file name fields is left blank I get an error like the following:

   Error Occurred While Processing Request
   The form field "form.attachedFileOne" did not contain a file.

If all three file fields are completed, I get this error:

   Error Occurred While Processing Request
   The resource listoffile was not found.
   The root cause was: .

I am a newbie when it comes to ColdFusion.  the code I have was cobbled together from Google and speaking with a fellow employee ( who I cannot bug too much)

Previously I was getting the error:

   The form field "D:\etc\etc\neotmp41503.tmp" did not contain a file.

which I fear may reappear when the current problem is resolved.  I have been assured that I do have access to the server where the files are stored after submittal, so I a mystified by the last problem as well.

The relevant code from the "sent" page is attached

I know this is complicated and I appreciate the help.

Cliff
<cfset FILEPATH = "D:\Web\WWW\CDD\et\bike\forms\images\">
<cfset listoffile = "">
<cfif Len(form.attachedFileOne) Is Not "">
	<cffile action="upload" filefield="form.attachedFileOne" destination="#FilePath#" nameconflict="MakeUnique">
	<cfset listoffile = listappend(listoffile, "#FILEPATH##FILE.SERVERFILE#")>
</cfif>
<cfif Len(form.attachedFileTwo) Is Not "">
	<cffile action="upload" filefield="form.attachedFileTwo" destination="#FilePath#" nameconflict="MakeUnique">
	<cfset listoffile = listappend(listoffile, "#FILEPATH##FILE.SERVERFILE#")>
</cfif>
<cfif Len(form.attachedFileThree) Is Not "">
	<cffile action="upload" filefield="form.attachedFileThree" destination="#FilePath#" nameconflict="MakeUnique">
	<cfset listoffile = listappend(listoffile, "#FILEPATH##FILE.SERVERFILE#")>
</cfif>
 
<cfmail to="ccook@cambridgema.gov" from="web@cambridgema.gov" subject="Bicycle Rack Suggestion">
 
	The following information was submitted on the Bike Rack placement form.
	
	Address of Suggested Location: #Form.address#
	Description of Suggested Location: #Form.location#
	Name: #Form.name#
	Phone Number: #Form.phone#
	Email Address: #Form.email#
	
	<cfif Len(listoffile) Is Not "">
		<cfloop list="listoffile" index="i">
			<cfmailparam file = "#i#">
		</cfloop>
	</cfif>
</cfmail>

Open in new window

0
ccook13
Asked:
ccook13
  • 3
  • 2
2 Solutions
 
azadisaryevCommented:
unless you are still on CF4.5 you should be using CFFILE.SERVERFILE instead of FILE.SERVERFILE - FILE prefix has been long deprecated. but that is not the cause of your troubles.

cf Len() function returns length of string in CHARACTERS. so it will NEVER be "" (empty string) - it is always a NUMBER.
so what you should be checking for in your cfif's is:
<cfif Len(Trim(form.attachedFileOne))> (and same for other file fields)  and <cfif listlen(listoffile)>

the other error you mentioned you had before - "The form field "D:\etc\etc\neotmp41503.tmp" did not contain a file." - was because you must have used # in the filefield attribute of cffile tag: using filefield="#form.attachedFileOne#" instead of filefield="attachedFileOne" (yes, you DO NOT need to prefix it with FORM.) will give you that error because it needs the name of the form field containing the file, not the name of the temp file created by cf on upload.

hth

0
 
ccook13Author Commented:
hth

Thanks for your for help so far.  I was out of the office for the last day so I am getting back to the issue this morning.

I made the changes you suggested.

I am no longer getting the error "The form field "form.attachedFileOne" did not contain a file." I also confirmed that the attached files are being placed on the server.

However, I am still getting the second error:

   Error Occurred While Processing Request
   The resource listoffile was not found.
   The root cause was: .

The modified code is attached.  Any help or guidance you offer will be appreciated.

Cliff

<cfset FILEPATH = "D:\Web\WWW\CDD\et\bike\forms\images\">
<cfset listoffile = "">
<cfif Len(Trim(form.attachedFileOne)) GT 0>
	<cffile action="upload" filefield="form.attachedFileOne" destination="#FilePath#" nameconflict="MakeUnique">
	<cfset listoffile = listappend(listoffile, "#FILEPATH##CFFILE.SERVERFILE#")>
</cfif>
<cfif Len(Trim(form.attachedFileTwo)) GT 0>
	<cffile action="upload" filefield="form.attachedFileTwo" destination="#FilePath#" nameconflict="MakeUnique">
	<cfset listoffile = listappend(listoffile, "#FILEPATH##CFFILE.SERVERFILE#")>
</cfif>
<cfif Len(Trim(form.attachedFileThree)) GT 0>
	<cffile action="upload" filefield="form.attachedFileThree" destination="#FilePath#" nameconflict="MakeUnique">
	<cfset listoffile = listappend(listoffile, "#FILEPATH##CFFILE.SERVERFILE#")>
</cfif>
 
<cfmail to="ccook@cambridgema.gov" from="web@cambridgema.gov" subject="Bicycle Rack Suggestion">
 
	The following information was submitted on the Bike Rack placement form.
	
	Address of Suggested Location: #Form.address#
	Description of Suggested Location: #Form.location#
	Name: #Form.name#
	Phone Number: #Form.phone#
	Email Address: #Form.email#
	
	<cfif listlen(listoffile) GT 0>
		<cfloop list="listoffile" index="i">
			<cfmailparam file = "#i#">
		</cfloop>
	</cfif>
</cfmail>
<cfmail to="ccook@cambridgema.gov" from="web@cambridgema.gov" subject="Bicycle Rack Suggestion">
	The following information was submitted on the Bike Rack placement form.
	
	Address of Suggested Location: #Form.address#
	Description of Suggested Location: #Form.location#
	Name: #Form.name#
	Phone Number: #Form.phone#
	Email Address: #Form.email#
</cfmail>

Open in new window

0
 
azadisaryevCommented:
when you loop over a list/array, you need to enclose the variable containing the list/array in the LIST or ARRAY argument of <cfloop> in #.
thus change:
<cfloop list="listoffile" index="i">

to:
<cfloop list="#listoffile#" index="i">

hth [a.k.a hope this helps]
0
 
azadisaryevCommented:
PS: i would also suggest adding the appropriate TYPE attribute to your <cfmailparam> tag.

you can programmatically get the MIME type of the uploaded file via #cffile.contenttype# and #cffile.contentsubtype#
0
 
ccook13Author Commented:
Thanks for the help.  The page is now up and running at:

http://www.cambridgema.gov/cdd/et/bike/forms/bike_rack_form.cfm
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now