by
Kyle Murray aka Krilnon |
3 January 2007We now need to deal with errors that may be generated
from the application we were coding on the
previous page.
Since this application depends largely on a remote server,
errors are somewhat harder to predict. Thankfully, most of
them can be dealt with, and Flash can report what sorts of
errors are occurring.
One of the more common errors is a permission error. Your
server may allow an upload, but the script that you used to
upload the file may not have permission to store that file
in a directory on the server. Unfortunately, this sort of
error isn't passed on to Flash, so test your upload script
thoroughly before deploying it anywhere. Using Apache, this
sort of error can be solved by changing the chmod of the
folder to allow 'Other' the permission to write (I used
757). Similar permission settings can be changed using on a
Windows server through IIS.
A little over a year ago, the
previous change was the only one that I had to make.
Several server configuration changes later, I noticed
that file uploads weren't working any longer. Thanks to a
note on the Adobe LiveDocs, I was able to fix the
issue by adding the following lines to my .htaccess
file:
SecFilterEngine Off
SecFilterScanPOST Off
Beware, the completion event fires before this error is
thrown, so be sure that your code can handle such an
error.
The error handling in this tutorial is contained within a
single function. It would be possible to handle each of the
three types of errors separately, but the error response in
this case will be so similar for each type that they group
logically into one function:
- function
handleError(errorName:String,
detail:Object):Void
{
- restart();
- if
(arguments.length
===
2)
{
- if
(typeof
detail
===
'number')
{
- display_txt.text
=
'HTTP Error #'+detail;
- }
else
{
- display_txt.text
=
'Security Error: '+detail;
- }
- }
else
{
- display_txt.text
=
'IO Error';
- }
- }
The application resets itself when an error occurs, just
as it did when it completed successfully. This way, the user
can try again and hopefully avoid the error. The conditional
statements following the restart check the type of error.
Conveniently, the three kinds of errors all pass slightly
different arguments into the handler. An IOError only passes
one argument, so it is easily identified by checking the
length of the arguments array accessible from every
function. HTTP and IO errors both pass two arguments, but an
HTTPError passes the second argument as a Number, not a
String. This difference can be detected using the typeof
operator. In any case, the application simply passes along
all of the information it is given to the user.
Hopefully this tutorial has served as a useful
introduction to the upload features of the FileReference
class. As always, the
kForums are available for questions, and I always try to
respond to my PMs (though the forums are usually a more
effective means of getting help).
|