views:

239

answers:

2

Is there a way to get a file's mimetype in Flex SDK 3.5 without using its extension?

I need to validate if an uploaded file is of a certain kind. This is for images, or documents (PDF, ODT, etc.)

All the solutions I've found are by checking its extension. What if I rename a .odt file as a .jpg? Then I can upload it as an image...

I should add, we are using an AIR desktop client and a Java EE server. File checking is solved on the Java side, but the idea is not to go to the server, validate the file so if it's not valid, there's no network traffic at all.

A: 

Easiest way would be using php, if possible:

$fileinfo['content-type'];
Steav
We are using Flex and Java EE. The server side checking is done, but I need to check the file on Flex, so I don't unnecessarily send it to the server.
Fernando
+2  A: 

Not really. Files do not have an inherent MIME type. The MIME type of arbitrary content data is described in the header of the internet protocol used to transport the data (such as HTTP, SMTP, RTP, etc.).

The only other solution I could think of off the top of my head is using a trial-and-error process where you have a guess at the file type you're dealing with, and you test that guess by actually opening the file (in your code) and testing for success. But that's ugly.

Steav, your solution just looks at the response header, which might not be set correctly.

Matt Ball