18

如果在使用 Uploadify 上传文件时服务器返回错误(HTTP 响应代码!= 200),则上传的文件将变为红色背景并显示如下消息:

file.jpg (52.78KB) - HTTP Error

表示存在 HTTP 错误。但这对用户来说不是很有用。如何让它显示更详细的错误消息?比如:“不是有效的图像”或“配额已满”?

我正在考虑在 HTTP 响应正文中传递这些消息,但 Uploadify 没有接收它们。是否有已知的方法将错误消息传递回 Uploadify?

4

4 回答 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;
?>
于 2010-01-07T20:01:14.707 回答
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);
        }
于 2013-01-18T19:11:45.370 回答
2

我有同样的问题。经过几个小时的搜索,我发现了问题。我在“internet Options->Lan setting”中设置了“代理服务器”,当我将其恢复为默认状态时,uploadify 再次工作。

于 2013-01-23T02:43:49.993 回答
1

对于 uploadify 3.0+ 版本,请查看onUploadSuccess选项 - 特别是传入的名为 data 的变量 - 它将具有服务器回显的任何内容。如果你回显 JSON,记得像这样解码它:

...
'onUploadSuccess' : function(file, data, response) {
    if (response){
        var json_data=JSON.decode(data);
        /* code here */
    }
},
....
于 2012-11-15T11:34:26.633 回答