CFFILE UPLOAD ... OPTIONAL?

I want to make the upload field optional

Page 1 of 1

6 Replies - 2554 Views - Last Post: 12 February 2009 - 12:55 PM Rate Topic: -----

#1 armyCoder  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 02-February 09

CFFILE UPLOAD ... OPTIONAL?

Posted 11 February 2009 - 07:02 AM

Right now I've got a page with an form to upload, but don't want to require a file to be uploaded. If I don't upload anything, I get the good ol' "not defined" error from CF. Is there any way to make the upload field optional?

<cffile action="upload" filefield="ul_path" nameconflict="makeunique" destination="c:\coldfusion8\wwwroot\GEPRS\upload\uploadtemp">
<cfset request.clientfile = CFFile.ClientFile>
<cfset request.filename="#request.clientfile#">

<cfswitch expression="#techRev#">
  <cfcase value="1">
  <cfset request.filename="#request.clientfile#">
  <cfquery name="updatetr1" datasource="GEPRS">
UPDATE reviews
SET tr1finish='1', tr1comments='#trcomments#', tr1file='#request.filename#'
WHERE reviewID=#URL.reviewID#
</cfquery>
  <cfif DirectoryExists("c:\coldfusion8\wwwroot\GEPRS\upload\#rf.reviewid#\tr1\")>
	<cffile action="MOVE" 
		source="c:\coldfusion8\wwwroot\GEPRS\upload\uploadtemp" 
		destination="c:\coldfusion8\wwwroot\GEPRS\upload\#rf.reviewid#\tr1\#request.filename#" 
		nameConflict="makeunique">
	<cfelse>
	<cfdirectory action = "create" directory = "c:\coldfusion8\wwwroot\GEPRS\upload\#rf.reviewid#\tr1\"> 
	<cffile action="MOVE" 
		source="c:\coldfusion8\wwwroot\GEPRS\upload\uploadtemp" 
		destination="c:\coldfusion8\wwwroot\GEPRS\upload\#rf.reviewid#\tr1\#request.filename#" 
		nameConflict="makeunique">
		
  </cfif>
  </cfcase>
</cfswitch>



  



<cfswitch expression="#techRev#">
  <cfcase value="2">
  <cfset request.filename="#request.clientfile#">
  <cfquery name="tr2" datasource="GEPRS">
UPDATE reviews
SET tr2finish='1', tr2comments='#trcomments#', tr2file='#request.filename#'
WHERE reviewID=#URL.reviewID#
</cfquery>
  <cfif DirectoryExists("c:\coldfusion8\wwwroot\GEPRS\upload\#rf.reviewid#\tr2\")>
	<span class="reviewid">
	  
	  <cffile action="MOVE" 
		source="c:\coldfusion8\wwwroot\GEPRS\upload\uploadtemp" 
		destination="c:\coldfusion8\wwwroot\GEPRS\upload\#rf.reviewid#\tr2\#request.filename#" 
		nameConflict="makeunique">
	  <cfelse>
	  <cfdirectory action = "create" directory = "c:\coldfusion8\wwwroot\GEPRS\upload\#rf.reviewid#\tr2\"> 
	  <cffile action="MOVE" 
		source="c:\coldfusion8\wwwroot\GEPRS\upload\uploadtemp" 
		destination="c:\coldfusion8\wwwroot\GEPRS\upload\#rf.reviewid#\tr2\#request.filename#" 
		nameConflict="makeunique">
	  </span>
  </cfif>
  </cfcase>
</cfswitch>


This post has been edited by armyCoder: 11 February 2009 - 07:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: CFFILE UPLOAD ... OPTIONAL?

#2 Ennio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-May 08

Re: CFFILE UPLOAD ... OPTIONAL?

Posted 11 February 2009 - 07:38 AM

Make sure you check to see if they uploaded a file. If they uploaded the file do the file upload if not do something else.
Was This Post Helpful? 0
  • +
  • -

#3 armyCoder  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 02-February 09

Re: CFFILE UPLOAD ... OPTIONAL?

Posted 11 February 2009 - 08:17 AM

View PostEnnio, on 11 Feb, 2009 - 07:38 AM, said:

Make sure you check to see if they uploaded a file. If they uploaded the file do the file upload if not do something else.


Ok so I should be able to do a <cfif isDefined> ?
Was This Post Helpful? 0
  • +
  • -

#4 Ennio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-May 08

Re: CFFILE UPLOAD ... OPTIONAL?

Posted 11 February 2009 - 08:50 AM

yes, check if ul_path was defined

<cfif isDefined('Form.ul_path')>
....
<cfelse>
....
</cfif>


Was This Post Helpful? 0
  • +
  • -

#5 sansclue  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: CFFILE UPLOAD ... OPTIONAL?

Posted 11 February 2009 - 12:33 PM

View PostEnnio, on 11 Feb, 2009 - 07:50 AM, said:

yes, check if ul_path was defined


IIRC, file fields are always defined. So IsDefined may not do the trick. Try checking the length of the field instead. If it is greater than 0, assume a file was supplied.


Though technically, checking the length is not enough to verify a file was uploaded.  A user could just type a random string like "sdfsdfsdf" into the file field.  So you should also use a cftry/cfcatch. If an error occurs display a message that there was a problem with the upload and abort.

<cfif len(trim(form.ul_path)) gt 0>
	 <cftry>
		  do the upload code here ....

		  <cfcatch>
			   Sorry. An error occurred.  Unable to upload the specified file
		  </cfcatch>
	 </cftry>
</cfif>


Was This Post Helpful? 1
  • +
  • -

#6 armyCoder  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 02-February 09

Re: CFFILE UPLOAD ... OPTIONAL?

Posted 12 February 2009 - 07:35 AM

View Postsansclue, on 11 Feb, 2009 - 12:33 PM, said:

View PostEnnio, on 11 Feb, 2009 - 07:50 AM, said:

yes, check if ul_path was defined


IIRC, file fields are always defined. So IsDefined may not do the trick. Try checking the length of the field instead. If it is greater than 0, assume a file was supplied.


Though technically, checking the length is not enough to verify a file was uploaded.  A user could just type a random string like "sdfsdfsdf" into the file field.  So you should also use a cftry/cfcatch. If an error occurs display a message that there was a problem with the upload and abort.

<cfif len(trim(form.ul_path)) gt 0>
	 <cftry>
		  do the upload code here ....

		  <cfcatch>
			   Sorry. An error occurred.  Unable to upload the specified file
		  </cfcatch>
	 </cftry>
</cfif>



That worked perfectly. Thanks.
Was This Post Helpful? 0
  • +
  • -

#7 sansclue  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: CFFILE UPLOAD ... OPTIONAL?

Posted 12 February 2009 - 12:55 PM

View PostarmyCoder, on 12 Feb, 2009 - 06:35 AM, said:

That worked perfectly. Thanks.


You are welcome!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1