jquery+ajax實(shí)現(xiàn)上傳圖片并顯示上傳進(jìn)度功能【附php后臺接收】
本文實(shí)例講述了jquery+ajax實(shí)現(xiàn)上傳圖片并顯示上傳進(jìn)度功能。分享給大家供大家參考,具體如下:
jquery上傳文件用的formdata,上傳進(jìn)度條需要添加xhr的onprogress
html代碼如下:
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <title>Ding Jianlong Html</title> <link rel="external nofollow" rel="stylesheet"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/layer/2.3/layer.js"></script> </head> <body> <!-- 外層div 進(jìn)度條的整體視覺和位置設(shè)置 --> <div style="width:300px;height: 20px;border: 1px solid #CCC;"> <!-- 內(nèi)層div 逐漸遞增的進(jìn)度條 --> <div id="jdt" style="height: 20px;"></div> </div> <p>總大小<span id="total"></span>;已上傳<span id="loaded"></span>;</p><br> <form id="mainForm"> 選擇文件:<input type="file" name="file"> <input type="button" value="上傳" onclick="upload()"> </form> <script> var uploading = false; function upload(){ //首先封裝一個(gè)方法 傳入一個(gè)監(jiān)聽函數(shù) 返回一個(gè)綁定了監(jiān)聽函數(shù)的XMLHttpRequest對象 var xhrOnProgress=function(fun) { xhrOnProgress.onprogress = fun; //綁定監(jiān)聽 //使用閉包實(shí)現(xiàn)監(jiān)聽綁 return function() { //通過$.ajaxSettings.xhr();獲得XMLHttpRequest對象 var xhr = $.ajaxSettings.xhr(); //判斷監(jiān)聽函數(shù)是否為函數(shù) if (typeof xhrOnProgress.onprogress !== 'function') return xhr; //如果有監(jiān)聽函數(shù)并且xhr對象支持綁定時(shí)就把監(jiān)聽函數(shù)綁定上去 if (xhrOnProgress.onprogress && xhr.upload) { xhr.upload.onprogress = xhrOnProgress.onprogress; } return xhr; } } var data = new FormData($('#mainForm')[0]); //要加【0】 console.log(data); if(uploading){ layer.alert("文件正在上傳中,請稍候"); return false; } $.ajax({ type: 'POST', url: 'upload_file.php', //當(dāng)前路徑 data: data, dataType: 'json', processData: false, //序列化,no contentType: false, //不設(shè)置內(nèi)容類型 beforeSend: function(){ uploading = true; }, //進(jìn)度條要調(diào)用原生xhr xhr:xhrOnProgress(function(evt){ var percent = Math.floor(evt.loaded / evt.total*100);//計(jì)算百分比 console.log(percent); // 設(shè)置進(jìn)度條樣式 $('#jdt').css('width',percent * 3 + 'px'); $('#jdt').css('background','skyblue'); //顯示進(jìn)度百分比 $('#jdt').text(percent+'%'); $('#loaded').text(evt.loaded/1024 + 'K'); $('#total').text(evt.total/1024 + 'K'); }), success: function (data) { if (data.code == 200) { layer.msg(data.message, {icon: 1, time: 1000}); //成功后關(guān)閉修改頁 setTimeout(function(){ var index = parent.layer.getFrameIndex(window.name); //先得到當(dāng)前iframe的索引 parent.layer.close(index); //在執(zhí)行關(guān)閉 } ,2000); //還有刷新下iframe的界面 parent.location.reload(); } else { layer.msg(data.message, {icon: 2, time: 3000}); } uploading = false; }, error: function (data) { alert('服務(wù)異常,請稍后重試'); console.log(data); } }); } </script> </body> </html>
php代碼如下:
<?php header('content-type:text/html;charset=utf-8'); if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { // 文件中文轉(zhuǎn)碼 //iconv('utf-8', 'gbk', $_FILES["file"]["name"]); //取出后綴名 $ext = strrchr($_FILES["file"]["name"],'.'); move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . uniqid() . $ext); $arr['code'] = 666; $arr['message'] = "已經(jīng)保存到: " . "upload/" . uniqid() . $ext; echo json_encode($arr);die; }
參考資料: http://www.dbjr.com.cn/article/94853.htm
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jquery中Ajax用法總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery常見經(jīng)典特效匯總》及《jquery選擇器用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計(jì)有所幫助。
相關(guān)文章
使用基于jquery的gamequery插件做JS乒乓球游戲
現(xiàn)在jquery比較流行,用js做游戲的也越來越多了,雖然現(xiàn)在html5出來了,但實(shí)際上要用html5做點(diǎn)啥出來還是得靠javascript,所以學(xué)好js是非常重要的2011-07-07基于jquery的文本框與autocomplete結(jié)合使用(asp.net+json)
基于jquery的文本框與autocomplete結(jié)合使用示例代碼,需要的朋友可以參考下2012-05-05jQuery簡單幾行代碼實(shí)現(xiàn)tab切換
本文給大家介紹的是一款使用jQuery實(shí)現(xiàn)的簡易選項(xiàng)卡的代碼,通過控制css熟悉來實(shí)現(xiàn)tab切換,思路清晰,這里推薦給大家。2015-03-03jQuery-App輸入框?qū)崿F(xiàn)實(shí)時(shí)搜索
這篇文章主要為大家詳細(xì)介紹了jQuery-App輸入框?qū)崿F(xiàn)實(shí)時(shí)搜索,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11jQuery基于擴(kuò)展實(shí)現(xiàn)的倒計(jì)時(shí)效果
這篇文章主要介紹了jQuery基于擴(kuò)展實(shí)現(xiàn)的倒計(jì)時(shí)效果,涉及jQuery擴(kuò)展的使用與時(shí)間操作的相關(guān)技巧,需要的朋友可以參考下2016-05-05