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 derfloh 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
Joined
Jun 11, 2001
Messages
177
Location
US
Hi all,

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

<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;. Im trying to do that with the following code:

<cfswitch expression=&quot;#right(form.picture1, 3)#&quot;>
<cfcase value=&quot;jpg&quot;>
cffile upload goes here
</cfcase>
<cfdefaultcase>
code goes here that displays message &quot;file is not a jpg&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;. Am I missing something here?
 
Use the accept attribute to restrict only jpeg uploads.

<input type=&quot;file&quot; name=&quot;picture1&quot; size=&quot;40&quot; accept=&quot;image/jpeg&quot;>

Note: FORM.picture1 will not contain text -> so you are not able to perform string operations correctly. It contains the complete file itself

Hope this helps :-)
 
but I want to extend the service to accept gifs too. I want to force a rename by specifying a new filename in the destination attribute. The cfswtch part is going to deal with whether the rename should have a .jpg on the end or a .gif on the end. Is there any way to get that info from form.picture?
 
Try this:

<form onSubmit=&quot;this.filename.value=this.picture1.value;&quot;>
<input type=&quot;file&quot; name=&quot;picture1&quot; size=&quot;40&quot;><BR>
<input type=&quot;hidden&quot; name=&quot;filename&quot; value=&quot;&quot;><BR>

<input type=&quot;submit&quot; value=&quot;submit&quot;>
</form>
 
I recommend using CFFILE to check/allow file type, then upload, and last to use the upload status parameters to rename.

The following file upload status parameters are available after an upload.

attemptedServerFile
Initial name ColdFusion used when attempting to save a file

clientDirectory
Directory location of the file uploaded from the client's system

clientFile
Name of the file uploaded from the client's system

clientFileExt
Extension of the uploaded file on the client's system without a period, for example, txt not .txt

clientFileName
Filename, without an extension, of the uploaded file on the client's system

contentSubType
MIME content subtype of the saved file

contentType
MIME content type of the saved file

dateLastAccessed
Date and time the uploaded file was last accessed

fileExisted
Indicates (Yes or No) whether or not the file already existed with the same path

fileSize
Size of the uploaded file

fileWasAppended
Indicates (Yes or No) whether ColdFusion appends the uploaded file to an existing file

fileWasOverwritten
Indicates (Yes or No) whether ColdFusion overwrites a file

fileWasRenamed
Indicates (Yes or No) whether the uploaded file is renamed to avoid a name conflict

fileWasSaved
Indicates (Yes or No) whether Cold Fusion saves a file

oldFileSize
Size of a file that was overwritten in the file upload operation

serverDirectory
Directory of the file saved on the server

serverFile
Filename of the file saved on the server

serverFileExt
Extension of the uploaded file on the server, without a period

serverFileName
Filename, without an extension, of the uploaded file on the server

timeCreated
Time the uploaded file was created

timeLastModified
Date and time of the last modification to the uploaded file

- tleish
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top