如果服务器在使用Uploadify上传文件时返回错误(HTTP响应代码!= 200),则上传的文件将显示红色背景,并显示如下消息:
file.jpg (52.78KB) - HTTP Error
Run Code Online (Sandbox Code Playgroud)
表示存在HTTP错误.但这对用户来说并不是很有用.如何让它显示更详细的错误消息?喜欢:'不是有效图像'或'配额满'?
我在考虑在HTTP响应体中传递这些消息,但是Uploadify没有接收它们.有没有已知的方法将错误消息传递回Uploadify?
看看uploadify论坛中关于如何处理错误的这两篇文章
那里有很多有用的信息..
更新
以下似乎为我做了诀窍..
'onComplete': function(a, b, c, d, e){
if (d !== '1')
{
alert(d);
}
else
{
alert('Filename: ' + c.name + ' was uploaded');
}
}
Run Code Online (Sandbox Code Playgroud)
加上这个版本的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;
?>
Run Code Online (Sandbox Code Playgroud)