Today I was in a conundrum. I was working on an upload script in AS3 that checks the file type and uploads it to a server. I had a problem with the FileReference class and Mac’s. (on Windows it was ok, but Mac’s did not work). According to the Actionscript 3 docs, all I need to do to check the type of a file is call the property type. ( see documentation: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/FileReference.html)
Normally the code would look like this:
var file:FileReference = new FileReference();
trace( file.type ); //where file.type would return the file type.
On a Mac machine, file.type returns null. However file.name returns the file name as it is supposed to. I could not find any real solutions, but instead just wrote a workaround class for this.
public static function extractFileType(fileName:String):String {
var extensionIndex:Number = fileName.lastIndexOf(".");
if (extensionIndex == -1) {
return "";
} else {
return "." + fileName.substr(extensionIndex + 1,fileName.length);
}
}
What this function does takes the file name and searches for the “.” and grabs the remaining characters returning the characters as a string. This imitates FileReference type property where it returns the file type as a string.
I have uploaded my simple class that can be downloaded here:
http://www.josh-ho.com/blog/Files/FileReferenceType.zip
Antago says:
Is nice, but this is not a class. It is a simple function. Classes are far more robust, portable, and complex.
Nov 17, 2009, 1:40 pmadmin says:
Yes I agree this is just a function and not a class. I built a set of simple functions for common uses (random number generator, complex string manipulations, image manipulations). It is easier to copy the files around and calling those files from project to project rather than copying code.
Nov 17, 2009, 11:23 am