18

If the server returns an error (HTTP response code != 200) when uploading a file with Uploadify, the uploaded file gets a red background and a message is show like this:

file.jpg (52.78KB) - HTTP Error

indicating that there was a HTTP Error. But that's not very useful to the the user. How can I make it show a more detailed error message? Like: 'Not a valid image' or 'Quota full'?

I was thinking of passing those messages in the HTTP response body, but Uploadify doesn't pick them up. Is there a known way to pass error messages back to Uploadify?

4 Answers 4

9

Take a look at these two posts in the uploadify forum on how to handle errors

onError to display what's happening and Upload script error reporting

there is a lot of useful info in there ..

Update

The following seems to do the trick for me ..

'onComplete': function(a, b, c, d, e){
                    if (d !== '1')
                        {
                        alert(d);
                        }
                    else
                        {
                        alert('Filename: ' + c.name + ' was uploaded');
                        }
                  }

coupled with this version of the uploadify script

<?php

    if (!empty($_FILES)) 
    {
        $tempFile = $_FILES['userfile']['tmp_name'];

        $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
        $targetFile =  str_replace('//','/',$targetPath) . $_FILES['userfile']['name'];

        move_uploaded_file($tempFile,$targetFile);

        switch ($_FILES['userfile']['error'])
        {     
            case 0:
             $msg = ""; // comment this out if you don't want a message to appear on success.
             break;
            case 1:
              $msg = "The file is bigger than this PHP installation allows";
              break;
            case 2:
              $msg = "The file is bigger than this form allows";
              break;
            case 3:
              $msg = "Only part of the file was uploaded";
              break;
            case 4:
             $msg = "No file was uploaded";
              break;
            case 6:
             $msg = "Missing a temporary folder";
              break;
            case 7:
             $msg = "Failed to write file to disk";
             break;
            case 8:
             $msg = "File upload stopped by extension";
             break;
            default:
            $msg = "unknown error ".$_FILES['userfile']['error'];
            break;
        }
    }
    if ($msg)
        { $stringData = "Error: ".$_FILES['userfile']['error']." Error Info: ".$msg; }
    else
        { $stringData = "1"; } // This is required for onComplete to fire on Mac OSX
    echo $stringData;
?>
Sign up to request clarification or add additional context in comments.

3 Comments

There doesn't seem to be a way to get at the HTTP response body. Thus providing more useful error messages from the server into the output is useless, since you can't display them.
Ok, I see it now... Only onComplete has access to the response body. onError has not but that is where I have been looking, since I wanted to show extra information, you know, in case of errors. But apparently onComplete also fires for errors? Thus allowing us to get at the response body. Why the developer of Uploadify didn't just pass the response to the onError function as well is beyond me...
After trying it, onComplete does not seem to fire in case of error. So as far as I see now, there is no way to get at the http response body in case of error. And thus, the question still stands. Time to contact the Uploadify author and ask him why he doesn't provide onError with access to the http response body...
4

Unfortunately the onUploadError event does not have access to the reponse body. You'll have to return 200 status and handle the errors in onUploadSuccess as far as I'm aware.

Here's how I'm doing it:

'onUploadSuccess' : function(file, data, response) {
            var responseObj = JSON.parse(data);
            if(responseObj.error_message)
            {
                $("#" + file.id).hide();   // this will hide the misleading "complete" message..
                alert(responseObj.error_message);
                return;
            }
        }

Or better yet you could replace the "complete" message with your error message like so:

 'onUploadSuccess' : function(file, data, response) {
            var responseObj = JSON.parse(data);
            if(responseObj.error_message)
            {
                $("#" + file.id).find('.data').css('color', 'red').html(' - ' + responseObj.error_message);
                return;
            }
            console.log(file, data, response);
        }

Comments

2

I've had the same problem. after search for hours I found the problem. I have set "proxy server" in my "internet Options->Lan setting" , and when I returned it to default state, uploadify worked again.

2 Comments

u didn't check, did u ? try using uploadify while u have set the proxy [wrong proxy], I've checked , uploadify didn't work. 100% it's related, maybe your problem was with your code, but mine was because of proxy.
Ooh sorry, you are right. I tried it without proxy and now I totally get the error message from the HTTP response. My (non-existent) proxy must have totally eaten that message in my earlier tests!
1

For uploadify version 3.0+ take a look at the onUploadSuccess option - specifically the passed in variable named data - that will have whatever the server echoed. If you echo JSON remember to decode it like so:

...
'onUploadSuccess' : function(file, data, response) {
    if (response){
        var json_data=JSON.decode(data);
        /* code here */
    }
},
....

3 Comments

Does this also trigger when the data was uploaded, but the server responded with a non 200 HTTP error code?
@hopla I don't think so, rather only on a 200 response. I believe non 200 HTTP codes could be caught with the onUploadError method
@Site, The onUploadError event does not have access to the reponse body. Has anyone resolved this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.