SWFUpload Error Handling
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)
-
cannot return data to the server, so it just returns a 500 error. For Flash Player 9 you will
-
want to change this to return the server data you want to indicate an error and then use SWFUpload's
-
uploadSuccess to check the server_data for your error indicator. */
-
function HandleError($message) {
-
header("HTTP/1.1 500 Internal Server Error");
-
echo $message;
-
}
-
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_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 = 'ERROR: File already exists on server';
Then my uploadSuccess function will look like this:
-
var temp = serverData.split(':');
-
-
if (temp[0] == 'SUCCESS') {
-
var progress = new FileProgress(file, this.customSettings.progressTarget);
-
progress.setComplete();
-
progress.setStatus(temp[1]);
-
progress.toggleCancel(false);
-
} else {
-
uploadError(file,-200, temp[1]);
-
}
-
}
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:
-
echo 'ERROR:'.$message;
-
}
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:
- Download SWF Examples – download the one called “SWFUpload v2.2.0 Beta 3 Samples.zip“
- SWFUpload API Documentation
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
