本文實(shí)例為大家分享了php實(shí)現(xiàn)ajax圖片上傳的具體代碼,供大家參考,具體內(nèi)容如下
html頁面代碼
<!DOCTYPE html> <html lang="en"> <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 class="form-horizontal" 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才會(huì)自動(dòng)加上正確的Content-Type */ contentType: false, /** * 必須false才會(huì)避開jQuery對(duì) formdata 的默認(rèn)處理 * XMLHttpRequest會(huì)對(duì) formdata 進(jìn)行正確的處理 */ 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']; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持。
【網(wǎng)站聲明】本站除付費(fèi)源碼經(jīng)過測(cè)試外,其他素材未做測(cè)試,不保證完整性,網(wǎng)站上部分源碼僅限學(xué)習(xí)交流,請(qǐng)勿用于商業(yè)用途。如損害你的權(quán)益請(qǐng)聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。