Ok this one is really exciting, I just spent the night working on it.
Unfortunately it’s for a client of mine so I’m not able to post the whole code and app, but, here’s how to proceed.
Cet article est en anglais, et en anglais uniquement, parce que c’est mon humeur du moment. Et aussi, j’ai la flemme de parler français.
INDEX
1. It’s all about…
You can handle file uploads with Flash.
It lets you create nice 2.0 interfaces with progressbars without using tricky iframes nor perl scripts.
It’s all done with FileReference.
But you can also use Flash to communicate with any script and it’s become easier since FlashEvents came with Flash 8 (or so).
2. What’s the point
The problem is, I’ve been crawling the web for nights and have never been able to find a script that could handle custom error messages.
Usually scripts are bundled with the Adobe-made methods, such as this one that handles bad requests :
file.addEventListener(HTTPStatusEvent.HTTP_STATUS, function(event:HTTPStatusEvent):void {; trace('oups...'); });
C’est bon, mais pas suffisant.
Since I perform all my upload checks through PHP I had the need to send strings TOFlash. And Flash should be able to try/catch these messages upon upload completion and inform the user that is upload is rejected.
Here’s how to connect them.
3. What does your server-side script should say ?
For the basic example, say that if your upload succeeds you echo « 1 » and if it fails you echo the reason why, prefixed by « error ».
(Yes, when it succeeds it’s cool and there’s nothing more to say, but I can see at least 7 points of failure so be prepared).
Echo it plain-text.
4. How can Flash handle custom error ?
We are going to use DataEvent.UPLOAD_COMPLETE_DATA.
First, any upload script already uses it. That’s less to type.
Second, using DataEvent.DATA or ProgressEvent.PROGRESS won’t do the job correctly, believe me I’ve tried. Moreover we want the error to be thrown after uploadbecause you that’s the way PHP handles most of (uncorrupted mime-types, real filesize on your box’s harddrive, links with your database, etc).
Ashtoningly, this is quite easy to do and I still can’t figure out why I spent so many hours trying.
(except because I suck at Flash)
// (...) // any script that performs the browsing, selecting, and uploading... // here we launch a listener to UPLOAD_COMPLETE_DATA with callback function fileCompleteHandler. Most upload scripts will already have something alike. file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, fileCompleteHandler); function fileCompleteHandler(event:DataEvent):void { if (event.text.substr(0,6) == 'erreur') { trace('erreur'); } else { trace('cool'); }
That’s it.
5. Real-life usage
To build a great interface you could plug Flash and jQuery together. I suggest that instead of throwing an error that only Flash will know of, you lanch a (possibly nice) javascript alert.
Let’s do it through jQuery because it rules. We need to use ExternalInterface to let Flash « talk to » jQuery.
// (sorry geshi's code highlighting's not the best so far...) function fileCompleteHandler(event:DataEvent):void { if (event.text.substr(0,7) == 'erreur_') { var erreur:Array = event.text.split("_"); ExternalInterface.call('$("#resultat").trigger("Your file has not been saved! Reason: "+erreur[1]+". Please try again. Or don't. Your decision."); } else { event.currentTarget.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA, fileCompleteHandler); }
Et voilà !