ThinkPHP+ajaxupload.js实现上传图片功能
应用的ThinkPHP版本为3.1.3
index.html模板页面代码如下nbsp;html><br>
<br>
<br>
<meta>
<br>
<title>图片上传</title>
<br>
<script></script><br>
<script></script><br>
<script><br />
$(function(){<br />
var button = $('#upload_button'), interval;<br />
var confirmdiv = $('#uploadphotoconfirm');<br />
var fileType = "pic",fileNum = "one"; <br />
new AjaxUpload(button,{<br />
action: "{:U('Index/uppic')}",<br />
name: 'userfile',<br />
onSubmit : function(file, ext){<br />
if(fileType == "pic")<br />
{<br />
if (ext && /^(jpg|png|jpeg|gif|JPG)$/.test(ext)){<br />
this.setData({<br />
'info': '文件类型为图片'<br />
});<br />
} else {<br />
confirmdiv.text('文件格式错误,请上传格式为.png .jpg .jpeg 的图片');<br />
return false; <br />
}<br />
}<br />
<br />
confirmdiv.text('文件上传中');<br />
<br />
if(fileNum == 'one')<br />
this.disable();<br />
<br />
interval = window.setInterval(function(){<br />
var text = confirmdiv.text();<br />
if (text.length < 14){<br />
confirmdiv.text(text + '.'); <br />
} else {<br />
confirmdiv.text('文件上传中'); <br />
}<br />
}, 200);<br />
},<br />
onComplete: function(file, response){<br />
if(response != "success"){<br />
if(response =='2'){<br />
confirmdiv.text("文件格式错误,请上传格式为.png .jpg .jpeg 的图片");<br />
}else{<br />
if(response.length>20){<br />
confirmdiv.text("文件上传失败请重新上传"+response); <br />
}else{<br />
confirmdiv.text("上传完成");<br />
$("#newbikephotoName").val("/ajaxup/upload/images/"+response);<br />
$("#newbikephoto").attr("src","/ajaxup/upload/images/"+response); <br />
}<br />
}<br />
<br />
}<br />
<br />
window.clearInterval(interval);<br />
<br />
this.enable();<br />
<br />
if(response == "success")<br />
alert('上传成功'); <br />
}<br />
});<br />
});<br />
<br />
<br />
</script><br>
<br>
<br>
<br>
<label> </label><br>
<input><br>
<input><br>
<input><br>
<div>
<br>
<img alt="ThinkPHP+ajaxupload.js实现上传图片功能" ><br>
<span></span><br>
<br><br>
<div></div>
<br>
<br><br>
<input><br><br>
<span>*请上传格式为.png .jpg .jpeg 的图片</span><br>
</div>
<br>
<br>
<br>
IndexAction.class.php代码如下:<?php <br />
/**<br>
* 图片上传<br>
* QQ:1739205253 中午有点风<br>
*/<br>
class IndexAction extends Action {<br>
public function index(){<br>
$this->display();<br>
}<br>
<br>
Public function uppic(){<br>
import('ORG.Net.UploadFile');<br>
$upload = new UploadFile();<br>
$upload->autoSub = true;<br>
$upload->subType = 'custom';<br>
if ($upload->upload('./upload/images/')){<br>
$info = $upload->getUploadFileInfo();<br>
}<br>
$file_newname = $info['0']['savename'];<br>
$MAX_SIZE = 20000000;<br>
if($info['0']['type'] !='image/jpeg' && $info['0']['type'] !='image/jpg' && $info['0']['type'] !='image/pjpeg' && $info['0']['type'] != 'image/png' && $info['0']['type'] != 'image/x-png'){<br>
echo "2";exit;<br>
}<br>
if($info['0']['size']>$MAX_SIZE)<br>
echo "上传的文件大小超过了规定大小";<br>
<br>
if($info['0']['size'] == 0)<br>
echo "请选择上传的文件";<br>
switch($info['0']['error'])<br>
{<br>
case 0:<br>
echo $file_newname;<br>
break;<br>
case 1:<br>
echo "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值";<br>
break;<br>
case 2:<br>
echo "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值";<br>
break;<br>
case 3:<br>
echo "文件只有部分被上传";<br>
break;<br>
case 4:<br>
echo "没有文件被上传";<br>
break;<br>
}<br>
}<br>
}
ThinkPHP+ajaxupload.js上传图片.zip ( 1.55 MB 下载:1155 次 )
AD:真正免费,域名+虚机+企业邮箱=0元