>I know I can get all kinds of information about a file being uploaded after
>its run through CFFILE action=upload, but I'm trying to figure out how to
>identify the file type BEFORE CFFILE has run.
Hi Howard. I recently created a simple document management system in which I
needed to know the mimetype before doing certain processing. The other poster
is correct in saying that your file will actually have already been uploaded by
the time your action page is called; however, you can still grab the mimetype
without having to handle the uploaded TMP file by doing a bit of JS on the
client side, as follows:
On the client side, the value of the FILE field actually contains the path and
name of the file that was selected, so we're leveraging a little JS here to
grab that value and stuff it into a hidden field to be passed on to our action
page, like so:
<HTML>...
<INPUT TYPE="file" ID="selectedFile" CLASS="button" NAME="selectedFile"
SIZE="50"
onChange="javascript:document.getElementById('filename').value=this.value;">
<INPUT TYPE="hidden" NAME="filename" ID="filename" VALUE="">
<INPUT TYPE="submit" NAME="Submit" VALUE="Add File" CLASS="button">
.....</HTML>
Within our action template, we parse the hidden field value to get the file
type (by extension) and name(in case we need to store that in our DB as well),
like so:
<HTML>...
<cfset variables.extension = UCase(listlast(listlast(form.filename,"\"),"."))>
<cfset variables.filename = GetFileFromPath(form.filename)>
.....</HTML>
I created a custom tag to handle my file processing (stuffing into my DB as a
blob, etc.), but the portion that will probably be of interest to you is how i
determine the mimetype before performing any real file processing. I have a
LIST of mimetypes and associated extensions which I stuff into a structure for
easy lookup of the submitted files type, like so:
<cfset mimelist =
"csv~application/csv,ez~application/andrew-inset,hqx~application/mac-binhex40,cpt~application/mac-compactpro,doc~application/msword,[shortened
for the post's sake...email me if you want my complete mime list]">
<cfset request.MIMETYPES = structnew()>
<cfloop list="#mimelist#" index="item">
<cfset tmp =
structinsert(request.MIMETYPES,listfirst(item,"~"),listlast(item,"~"))>
</cfloop>
I then use this structure (request.MIMETYPES) to validate that my incoming file
is of a known type, like so:
<!--- validate the mimetype against our extensive list --->
<cfif not structkeyexists(request.MIMETYPES,Attributes[thisvar])>
<cfset variables.throwmessage = "Parameter '" & thisvar & "' must contain a
valid mime type. You passed in '" & Attributes[thisvar] & "'.">
<cfthrow message=#variables.throwmessage# type="Application">
<cfexit>
</cfif>
If you'd like to see the entire tag or need any further input on how i
accomplished my task, let me know. :0)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Introducing the Fusion Authority Quarterly Update. 80 pages of hard-hitting,
up-to-date ColdFusion information by your peers, delivered to your door four
times a year.
http://www.fusionauthority.com/quarterly
Archive:
http://www.houseoffusion.com/cf_lists/message.cfm/forumid:4/messageid:245953
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4