在不使用jquery form插件的情况下可以使用iframe来实现图片的无刷新上传
HTML页面如下:
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="ajax_upload.php" target="check_file_frame" enctype="multipart/form-data" method="post" name="uploadForm">
<input type="file" οnchange="upload(this)" name='uploadFile'/>
<iframe id='check_file_frame' name='check_file_frame'></iframe>
</form>
<script>
function upload(fileObj) {
if(fileObj.value != '') {
document.uploadForm.action = 'ajax_upload.php';
document.uploadForm.submit();
}
return false;
}
</script>
</body>
</html>
PHP处理如下('ajax_upload.php'):
<?php
/**
* Created by PhpStorm.
* User: 源路苦行
* Date: 2017/1/14
* Time: 14:15
*/
$oldname = $_FILES["uploadFile"]["name"];
$newname = time().".jpg";
$type =$_FILES['uploadFile']['type'];
//图片大小为2M
if(!$_FILES['uploadFile']['size']>2097152){
echo "图片大小超过2M";
}
elseif($type!="image/jpeg" && $type!="image/png" ){
echo "图片类型错误";
}else{
if ( is_uploaded_file ( $_FILES [ 'uploadFile' ][ 'tmp_name' ])){
$url = 'weiBoImg/'.$newname;
if(move_uploaded_file($_FILES["uploadFile"]["tmp_name"],$url)){
echo "<img src='weiBoImg/$newname' alt=''/>
<p>图片描述:<input type='text' name='miaosu'/></p>";
}else{
echo "移动失败!";
}
}else{
echo "文件不存在了!";
}
}