Heim  >  Artikel  >  php教程  >  ThinkPHP+ajaxupload.js实现上传图片功能

ThinkPHP+ajaxupload.js实现上传图片功能

WBOY
WBOYOriginal
2016-06-07 11:41:121167Durchsuche

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 = $(&#039;#upload_button&#039;), interval;<br /> var confirmdiv = $(&#039;#uploadphotoconfirm&#039;);<br /> var fileType = "pic",fileNum = "one"; <br /> new AjaxUpload(button,{<br /> action: "{:U(&#039;Index/uppic&#039;)}",<br /> name: &#039;userfile&#039;,<br /> onSubmit : function(file, ext){<br /> if(fileType == "pic")<br /> {<br /> if (ext && /^(jpg|png|jpeg|gif|JPG)$/.test(ext)){<br /> this.setData({<br /> &#039;info&#039;: &#039;文件类型为图片&#039;<br /> });<br /> } else {<br /> confirmdiv.text(&#039;文件格式错误,请上传格式为.png .jpg .jpeg 的图片&#039;);<br /> return false; <br /> }<br /> }<br /> <br /> confirmdiv.text(&#039;文件上传中&#039;);<br /> <br /> if(fileNum == &#039;one&#039;)<br /> this.disable();<br /> <br /> interval = window.setInterval(function(){<br /> var text = confirmdiv.text();<br /> if (text.length < 14){<br /> confirmdiv.text(text + &#039;.&#039;); <br /> } else {<br /> confirmdiv.text(&#039;文件上传中&#039;); <br /> }<br /> }, 200);<br /> },<br /> onComplete: function(file, response){<br /> if(response != "success"){<br /> if(response ==&#039;2&#039;){<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(&#039;上传成功&#039;); <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元

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn