views:

374

answers:

3

My users are going to be uploading files with a .EXP extension. In ColdFusion on Windows 2003 I'm using getPageContext().getServletContext().getMimeType() to make sure that the file that they upload is of the correct mime-type, which will be text/plain. The issue I'm having is that no matter where I register the mime-type on the server, getPageContext().getServletContext().getMimeType() will return blank since it doesn't know about the .EXP file extension. What is the trick to get ColdFusion to see this file extension.

A: 

the MIME types supported are defined in your webserver, whether it's IIS, Apache or other. Apache MIME types can be configured in the mime.types files in the httpd/conf directory. The getMimeTypes() method communicates with the web server and returns the associated MIME type for the file.

http://chris.cfwebtools.com/index.cfm/2009/8/12/Securely-Serving-Files-via-CFContent

Henry
i've actually tried that it still doesn't pick it up.
rip747
@Henry: AFAIK, the MIME type you set in the server only affects downloads, not uploads.
Tomalak
For uploads, MIME type is provided by the Browser, I believe, and Browser determine MIME type by file extension only, so it is not trustworthy.
Henry
A: 

How about simply checking the request headers for the MIME type?

<cfset hrd = GetHttpRequestData()>
<cfdump var="#hrd#">

You'll get a struct that contains all data the client sent, including the content type of the uploaded file.

In any case, trusting the MIME type or the file extension the client sends you is dangerous, I would rather check the file with some other means (RegEx, for example, or a parser) to make sure it is what you expect.

Tomalak
trying to invoke GetRequestData() returned and error stating that it was a valid function.
rip747
Whoops. My bad, I screwed up the function name. Corrected.
Tomalak
Yeah. Uncommented down-votes are the best.
Tomalak
+1  A: 

FINALLY!!!!

In order for getPageContext().getServletContext().getMimeType() to recognize the new filetype, you must edit the file:

<ColdFusion-home>\runtime\lib\mime.types

In my case this file was located at:

C:\ColdFusion8\runtime\lib\mime.type

I opened file file and found this line:

text/plain asc txt

All I had to do was add my extension to the end like so:

text/plain asc txt exp

Then I restart the ColdFusion service and invoking getPageContext().getServletContext().getMimeType() return text/plain.

rip747
if anyone knows of a way to register a new mime type by using java and not editing the file directly, PLEASE let me know. while this is a fix, it won't be possible to do this on a shared server.
rip747