昨天弄了一下午,可谓的错误接连不断,心情莫名烦燥了,心理素质有待提高。。。做此功能:利用ajax上传文件,处理文件,返回文件名,再和其它域一块提交
光ajaxfileupload.js插件就找了好几个,老报错,总感觉是插件下载的不对。。
html部分:
<html>
<head>
<title>Ajax File Uploader Plugin For Jquery</title>
<meta http-equiv="Content-Type" content="text/html"; charset="utf-8">
<link href="ajaxfileupload.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="ajaxfileupload.js"></script>
<script type="text/javascript">
function ajaxFileUpload()
{
$.ajaxFileUpload
(
{
url:'doajaxfileupload.php',
secureuri:false,
fileElementId:'fileToUpload',
dataType: 'json',
data:{name:'logan', id:'id'},
success: function (data, status)
{
if(typeof(data.error) != 'undefined')
{
if(data.error != '')
{
alert(data.error);
}else
{
$("#file_name").val(data.msg);
alert(data.msg);
}
}
},
error: function (data, status, e)
{
alert(e);
}
}
)
return false;
}
</script>
</head>
<body>
<div id="wrapper">
<div id="content">
<h1>Ajax File Upload Demo</h1>
<img id="loading" src="loading.gif" style="display:none;">
<form name="form" action="" method="POST" enctype="multipart/form-data">
<input type="text" name="username" />
<input id="fileToUpload" type="file" size="45" name="fileToUpload" class="input">
<button class="button" id="buttonUpload" onclick="return ajaxFileUpload();">Upload</button>
<input type="hidden" id="file_name" name="file_name" value="" />
</form>
</div>
</body>
</html>
doajaxfileupload.php:
<?php
$error = "";
$msg = "";
$fileElementName = 'fileToUpload';
$filename=$_FILES['fileToUpload']['name'];
$arr=explode('.',$filename);
$last='.'.array_pop($arr);
$new_name=uniqid().time().$last;
if(!empty($_FILES[$fileElementName]['error']))
{
switch($_FILES[$fileElementName]['error'])
{
case '1':
$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
break;
case '2':
$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
break;
case '3':
$error = 'The uploaded file was only partially uploaded';
break;
case '4':
$error = 'No file was uploaded.';
break;
case '6':
$error = 'Missing a temporary folder';
break;
case '7':
$error = 'Failed to write file to disk';
break;
case '8':
$error = 'File upload stopped by extension';
break;
case '999':
default:
$error = 'No error code avaiable';
}
}elseif(empty($_FILES['fileToUpload']['tmp_name']) || $_FILES['fileToUpload']['tmp_name'] == 'none')
{
$error = 'No file was uploaded..';
}elseif(!move_uploaded_file($_FILES['fileToUpload']['tmp_name'],'./test/'.$new_name)){
//file_put_contents('a.txt',$_FILES['fileToUpload']['tmp_name']);
$error='移动不成功';
}else {
//$msg .= " File Name: " . $_FILES['fileToUpload']['name'] . ", ";
//$msg .= " File Size: " . @filesize($_FILES['fileToUpload']['tmp_name']);
$msg=$new_name;
@unlink($_FILES['fileToUpload']);
}
echo "{";
echo "error: '" . $error . "',\n";
echo "msg: '" . $msg . "'\n";
echo "}";
?>
关于报错的一些问题:
jQuery.handleError is not a function 经测试handlerError只在jquery-1.4.2之前的版本中存在,jquery-1.6 和1.7中都没有这个函数了,这个比较蛋疼。。