Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help with <cfswitch> and <cfcase>! 1

Status
Not open for further replies.

leadman

Programmer
Jun 11, 2001
177
US
Hi all,

As part of trying to let users upload a jpg or gif file, I created a form with the following file field:

Code:
<input type=&quot;file&quot; name=&quot;picture1&quot; size=&quot;40&quot;>

When the form is submitted, I want to check if the last 3 places in the &quot;picture1&quot; file field are &quot;jpg&quot; or &quot;gif&quot;. Im trying to do that with the following code:

Code:
<cfswitch expression=&quot;#right(form.picture1, 3)#&quot;>
<cfcase value=&quot;jpg&quot;>
cffile upload goes here
</cfcase>
<cfcase value=&quot;gif&quot;>
cffile upload goes here
</cfcase>
<cfdefaultcase>
code goes here that displays message &quot;file is not a jpg or gif&quot;
</cfdefaultcase>
</cfswitch>

For some reason it is always going to the default case, even when the file ive chosen ends in &quot;jpg&quot; or &quot;gif&quot;. Am I missing something here?
 
You cannot do this, as I told you earlier, it is not sent in as a string but as the complete file.

Another solution would be to finish uploading the file to the server using CFFILE and then check it.

ie. do the CFFILE ACTION=&quot;Upload&quot;

Then check the #FILE.ContentType# variable for image/jpeg or image/gif.

If is not as expected, delete the file on the server and show the error message. Else proceed.

Hope this helps.
 
gana78,

The value of form.picture1 is the complete filespec, NOT the contents of the file.

leadman,

If it is always going to the default case, you know it's not matching any of the other cases. How about adding a debugging write just before the switch statement to verify that you are getting the right values:

<cfoutput>file ext.=#htmleditformat(right(form.picture1, 3))#<br></cfoutput>

It possible that there are trailing spaces on the filename.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top