摘要: 使用iframe來處理異步上傳圖片,在現在這個時代來說,多多少少都有點落后了!單單就憑AJAX和JS就不能做到異步上傳圖片了嗎?
感謝 think2011 這位兄臺的JS庫:https://github.com/think2011/LocalResizeIMG
先看調用頁面:
<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no"> <script type="text/javascript" src="./js/lrz.mobile.min.js"></script> <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script></head><body class="upload"><form id="form"> <div id="img_show"></div> <div id="upload"> <div id="img_file"><input type="file" accept="image/*" ><div class="btn">選擇圖片</div></div> </div> <input type="submit" class="tijiao" value="提交"> </form></body><script type="text/javascript"> var img; $("input:file").change(function (){ //console.log(this.files[0]); lrz(this.files[0],{width:640,quality:0.9},function(rst){ img = rst.base64; var html = []; var show_img = new Image(); show_img.src = rst.base64; $("#img_show").html("<div class='upimg'></div>"); $(".upimg").html(show_img); }); }); $("#form").submit(function (){ var phone = $("input[name='phone']").val(); var month = $("input[name='month']").val(); $.post("upload.php",{img:img,phone:phone,month:month},function(data){ img = null; alert(data.msg); },'json'); return false; });</script></html>1.首先你要載入JS類庫:
<script type="text/javascript" src="./js/lrz.mobile.min.js"></script>
2.然后就是寫好form
3.準備處理圖片以及圖片異步提交的JS。
<script type="text/javascript"> var img; $("input:file").change(function (){ //console.log(this.files[0]); lrz(this.files[0],{width:640,quality:0.9},function(rst){ img = rst.base64; var html = []; var show_img = new Image(); show_img.src = rst.base64; $("#img_show").html("<div class='upimg'></div>"); $(".upimg").html(show_img); }); }); $("#form").submit(function (){ var phone = $("input[name='phone']").val(); var month = $("input[name='month']").val(); $.post("upload.php",{img:img},function(data){ img = null; alert(data.msg); },'json'); return false; });</script>從代碼中可以看出,這個JS庫是把圖片轉成碼,然后用變量存起來,然后在用異步POST到服務器中在處理。
看起來貌似沒有什么特別的地方,的確實在也沒有什么特別的地方.......
后臺處理程序PHP:
function error($msg=''){ $return = array('msg'=>$msg); echo json_encode($return); exit();}function main(){ if(!$_POST['img']){ error('請上傳圖片!'); } $img = $_POST['img']; $path = './upload/'; $type_limit = array('jpg','jpeg','png'); if(preg_match('/data:/s*image//(/w+);base64,/iu',$img,$tmp)){ if(!in_array($tmp[1],$type_limit)){ error('圖片格式不正確,只支持jpg,jpeg,png!'); } }else{ error('抱歉!上傳失敗,請重新再試!'); } $img = str_replace(' ','+',$img); $img = str_replace($tmp[0], '', $img); $img = base64_decode($img); $file = $path.time().'.'.$tmp[1]; if(!file_put_contents($file,$img)){ error('上傳圖片失敗!'); }else{ error('恭喜您!上傳成功!'); }}main();上述代碼如果有錯誤歡迎指出。
如上訴代碼,正如你看到的那樣,經過BASE64加密過的圖片碼經過JS異步的POST過來后端后,我們要把代碼還原。但是JS庫加密的時候會帶有一些標簽,所以還原前需要處理掉這些本來不屬于圖片的東西。
$img = str_replace(' ','+',$img); $img = str_replace($tmp[0], '', $img);$img = base64_decode($img);最后把代碼塞進文件,設置好相應的文件名和擴展名,圖片就成功上傳到了服務器了。
注意:
前后端包括JS編碼要要一致,建議UTF-8
如果圖片還原不會來的話,那肯定是數據問題,打印POST過來的圖片碼出來看看。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答