html+php实现无刷新上传文件

亲测好用!!!代码示例是上传excel

html代码

<form id="uploadForm" class="picForm" action="finance.php?act=excel" method="post" enctype="multipart/form-data" target="hiddenIFrame">
<input type="file" class="excel" name="excel" style="border-radius: 5px;">
</form>
<iframe id='hiddenIFrame' name='hiddenIFrame' style="display:none;"></iframe>
js代码(jq)

$(function(){
$("#hiddenIFrame").load(function(){
var wnd = this.contentWindow;
var str = $(wnd.document.body).html();
callback(str);
});
})

function callback(info){
console.log(info)
if(info=='b')
{
alert('导入成功');
}
else if(info=='a')
{
alert('请选择正确的文件类型');
}
}

$(".excel").change(function(){
var _this=$(this);
$("#uploadForm").submit();
})
php代码
$excel=$_FILES['excel'];
$type=substr($excel['name'],strripos($excel['name'],'.')+1);
if($type!='xls' && $type!= 'xlsx')
{
echo 'a';
}
else
{
$date=strtotime(date("Y-m-d H:i:s"));
if(move_upload_file($excel['tmp_name'],'excel/'.$date.'.'.$type))
{
echo 'b';
}
}