好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

基于formdata属性php+ajax+h5实现图片上传功能

本文实例为大家分享了php实现ajax图片上传的具体代码,供大家参考,具体内容如下

html页面代码

<!DOCTYPE html> <html> <head>   <meta charset="UTF-8">   <title>Title</title>   <script type="text/javascript" src="__PUBLIC__/home/js/jquery-1.11.0.js"></script>   </head> <body> <form role="form" id="myForm"    action="/index/fileupsend" method="post"    enctype="multipart/form-data">     选择文件:<input type="file" id="file1" /><br />   <input type="button" id="upload" value="上传" />   <span id="imgWait"></span> </form> <script>   $(function () {     $("#upload").click(function () {       $("#imgWait").html("上传中");       var formData = new FormData();       formData.append("myfile", document.getElementById("file1").files[0]);       $.ajax({         url: "/Home/index/fileupsend",         type: "POST",         data: formData,         /**          *必须false才会自动加上正确的Content-Type          */         contentType: false,         /**          * 必须false才会避开jQuery对 formdata 的默认处理          * XMLHttpRequest会对 formdata 进行正确的处理          */         processData: false,         success: function (data) {           if(data){             alert("上传成功!");           }           $("#imgWait").html("上传成功");           },         error: function () {           alert("上传失败!");           $("#imgWait").hide();         }       });     });   }); </script> </body> </html>

php代码

public function fileupsend(){   $type_pic = $this->file_upload('1',array('jpg', 'gif', 'png', 'jpeg'),'filetest','myfile');   echo $type_pic['img_path'];   }

查看更多关于基于formdata属性php+ajax+h5实现图片上传功能的详细内容...

  阅读:73次

上一篇: php ajax 留言板 - 综合实例

下一篇:没有了