SWFUpload Error Handling

2008 November 21

We were looking for a way to change the errors that SWFUpload shows when something happens on the server. I went through the API docs on their website, eventually gave up, and resorted to reading through the code of their demos.

I had to put some piecemeal information in the demos together in order to figure out a way to make this happen.

First, we found that the example upload.php (samples/upload.php) could send a 500 error when the file upload failed. The example also was outputting an error message. The code looks like this: (note the comments)

/* Handles the error output. This function was written for SWFUpload for Flash Player 8 which
  1. cannot return data to the server, so it just returns a 500 error. For Flash Player 9 you will
  2. want to change this to return the server data you want to indicate an error and then use SWFUpload's
  3. uploadSuccess to check the server_data for your error indicator. */
  4. function HandleError($message) {
  5. header("HTTP/1.1 500 Internal Server Error");
  6. echo $message;
  7. }

What the comments there told me was there was a way to pass custom error messages to the flash object. Now to find those errors….

I started looking at the index.php file which creates the flash object, I found this:

upload_error_handler : uploadError,
  1.  upload_success_handler : uploadSuccess,

This is telling me that there is a function called uploadError and uploadSuccess that are called when those actions happen. This information with the comment above, now i’m looking in the JS.

So, I found the functions and played around with them a little bit. I found that when you pass any header other than a 200 from upload.php after an upload uploadError gets called. However, if you pass just test uploadSuccess gets called.

My solution was to pass text back no matter what and just parse out whether it is an error or success in uploadSuccess. My messages will look like one of these:

$message = 'SUCCESS: File Uploaded Successfully';
  1. $message = 'ERROR: File already exists on server';

Then my uploadSuccess function will look like this:

function uploadSuccess(file, serverData) {
  1.  var temp =  serverData.split(':');
  2.  
  3.  if (temp[0] == 'SUCCESS') {
  4.   var progress = new FileProgress(file, this.customSettings.progressTarget);
  5.   progress.setComplete();
  6.   progress.setStatus(temp[1]);
  7.   progress.toggleCancel(false);
  8.  } else {
  9.   uploadError(file,-200, temp[1]);
  10.  }
  11. }

I parse out what type of message it is, and act accordingly. On the call to uploadError I pass the file (passed to uploadSuccess), -200 for the type of error (an HTTP error), and temp[1] which is the message we want to display.

Almost done…

The last major change I did was to go back to the code in upload.php that we mentioned earlier and change all the messages to be in the format I specified before. Then I changed the code I showed earlier to this:

function HandleError($message) {
  1.  echo 'ERROR:'.$message;
  2. }

The only other change that I made to the code they made available was to remove all the debug stuff from handler.js once I was sure things were working correctly.

Files used from the sample code download:

  • demos/multiinstancedemo/index.php
  • demos/multiinstancedemo/js/*
  • samples/php/upload.php
  • demos/swfupload/*

Links we used:


We are glad this information is becoming so useful to people who are using SWFUpload. If possible, we would appreciate a link back to this post.

Sphere: Related Content

11 Comments leave one →
2009 January 13

after spending 1.5 hours reading the code … I came up with the exact same solution, only that my code is a bit different – had no choice. But your article adds confirmation, thanks a mil’ for sharing your solution, really appreciate it.
I’m still wondering how in the name of god these guys MAP those error codes with HTTP HEADERS … my best guess is that it’s somehow pre-coded in the SWF file ? (although it doesn’t make sense if it is hard coded there)

2009 January 13

We were utterly confused with this suite as well as absolutely loving it. So, I back tracked to where the error codes were being handled. It seems like they really made the flash app to manage the php apps and provide UI.

2009 January 19
Schmoohaa UNITED STATES Mac OS X Mozilla Firefox 3.0.5 permalink

New to SWFUpload. My main question may have been answered, but I am not sure. How do I pass back a specific error code from the server so that the uploadError callback will be fired and that the correct error displayed? Or do I need to screw around with the uploadSuccess and then call the error function like shown?

My issue is that I want the server side to totally control when the upper limit of uploads has happened and to stop allowing more. My fear is that by using the javascript-based configs to set the max amount that it can be manipulated (maybe an unwarrant fear). Regardless, I would like to be able to, for example, stop the 3rd out of 5 uploads because the user has reached a limit. Send back the “error” code and have SWFUpload handle the rest.

Thanks.

2009 January 20

I tried sending different server error codes like 500, 303, etc and they all gave me the same result with SWFUpload. So, I personally believe that the best way to get this to work is just to work with the response similar to how we did it.
You can always use and parse JSON or XML if you would like, this would give you more flexibility with your error codes.

2009 January 26

Thanks so much for this! Very helpful…

2009 February 3

I had been puzzling over how to get a server-side error back to the .js for several hours. Thanks for figuring it out and posting your findings … much appreciated!

2009 February 5
Arnski GERMANY Mac OS X Safari 525.27.1 permalink

Saved me some work. Thanks a lot.

(”< (”< *piep*
( ,,)( ,,)
” ”

2009 February 5
Arnski GERMANY Mac OS X Safari 525.27.1 permalink

A minor typo though:

It should be this.uploadError(file,-200, temp[1]); instead of uploadError(file,-200, temp[1]);, should’nt it?

Regards,

Arnski

2009 February 5

It seems to be working for us with the uploadError function from handlers.js in the package you get from SWFUpload

2009 April 1
Adnan PAKISTAN PHP permalink

I tried to fetch server data by mentioning 2nd argument in “uploadComplete” methond but its saying UNDEFINED

how can i debug the upload.php file?

2009 April 1

What I usually do is use PHP’s error_log(); function. This will output data to your php error log so you can see what is happening when you run your script. Most times I have found that one of two things are the issue if you set up the script correctly:
1. The folder you are moving files to either does not exist or has the wrong permissions
2. Your php.ini file has the temp folder set to system default and it isn’t able to find that folder. Try setting it to /tmp or /temp on *nix servers.

Leave A Comment

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS