rss
twitter
  • Showcase
  • Carrières
  • Support
  • GitLab
  • Espace client
  • Contact

Send custom data error to Flash when using FileReference

0

Ok this one is really exciting, I just spent the night working on it.Retour ligne automatique
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 ».Retour ligne automatique
(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.Retour ligne automatique
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.Retour ligne automatique
(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.Retour ligne automatique
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à !

DATE 06 Mar 2009
by : Germain
Author / Auteur

Social Share / Partager

    Leave a Reply / Répondre Annuler la réponse

    *
    *

    More in Back-end : PHP, CMS

    • Quel php.ini (MAMP ou Linux), quelle valeur de phpinfo() ?
    • Trucs utiles pour WordPress
    • Les bons headers PHP pour envoyer du CSV compatible Excel en UTF8
    • Imagemanager(tiny Mce) et kohana
    • Récupérer, vérifier et exploiter une date

    Search the blog

    Blog categories

    • Front-end : HTML, CSS (13)
    • Workflow (1)
    • Front-end : javascript, jQuery (33)
    • Back-end : PHP, CMS (42)
    • Back-end : SQL, MySQL (20)
    • Sysadmin : Linux, Apache, GIT (75)
    • Designers : tools, advice (2)
    • Desktop : OS X (37)
    • Desktop : Microsoft (12)
    • Uncategorized (8)

    ITALIC™ Resources

    • Paramètres de connexion au serveur mail
    • Graphistes : séduire un développeur web
    • Dév front : nos bonnes pratiques
    • Mailing : nos (bonnes ?) pratiques
    • Environnement de travail Mac

    Recent posts

    • Autoriser le chargement de contenus mixtes non-https dans Chrome (Mac)
    • Afficher les logs MySQL
    • Recevoir un mail lors de la connexion root à un serveur
    • Changer l’interclassement de plusieurs tables MySQL à la fois (ou presque
    • Maildev : tester l’envoi de mails durant ses développements
    © 2008-2017 ITALIC™ • 8 bis rue Abel • 75012 PARIS • Tel +33 (0)1 48 44 46 35 • RCS PARIS 508 228 772 • Powered by WordPress & GoodLayers
    Nous utilisons des cookies pour vous garantir la meilleure expérience sur notre site web.
    Cookie settingsACCEPTER
    Manage consent

    Privacy Overview

    This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
    Necessary
    Toujours activé
    Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
    Non-necessary
    Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
    Enregistrer & appliquer