FileReference woes on MACs
Today I found out (the hard way) that there are several issues that can occur when trying to upload files using the FileReference class on MACs.
FileReference.type always returns null
It’s often that case that you want to restrict the file type the user can upload, or perform different processes depending on the type of file uploaded. However the type property of a FileReference object always returns null on MACs; to work around this you must get the file extension manually. To do this you can use the following code (it’s good practice to always convert the file extension to lower case before working with it).
Server script must return data
When uploading files using the FileReference class you may experience the upload getting stuck at 100% progress and never firing the Event.COMPLETE event. This will only happen on the MAC and it’s because Flash doesn’t realise that the file has finished uploading until it gets a response from the server. I don’t why this is implemented differently on the different operating systems, but to overcome this problem your server script must write something to the page (it can just be a space character!).
Parameters must be added using URLVariables
You may want to pass URL parameters to the server script when uploading a file, and in our case the parameters and the URL were passed as one string via Flash vars. MACs don’t like this. So instead we have to pass the parameters using the data property of the URLRequest; to do this you need to use the URLVariables class. The following code snippet shows you how.
Feedback!
Has anyone else come across any weird quirks or handy hints when working with the FileReference class? Please leave a comment if you have and I’ll add it to the post!
