Uploadify:显示来自HTTP响应的错误消息

时间:2009-12-09 23:19:29

标签: uploadify

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

file.jpg (52.78KB) - HTTP Error

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

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

4 个答案:

答案 0 :(得分:9)

在uploadify论坛中查看这两篇关于如何处理错误的帖子

onError to display what's happeningUpload script error reporting

那里有很多有用的信息..

<强> 更新

以下似乎为我做了诀窍......

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

加上此版本的uploadify脚本

<?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;
?>

答案 1 :(得分:4)

不幸的是,onUploadError事件无权访问响应主体。根据我的意识,你必须返回200状态并处理onUploadSuccess中的错误。

以下是我的表现:

'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;
            }
        }

或者更好的是,您可以将错误消息替换为“完整”消息,如下所示:

 '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);
        }

答案 2 :(得分:2)

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

答案 3 :(得分:1)

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

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