效果圖:
javascript
直接上代碼了、html
<div class="main-wrap"> <div class="profile"> <p class="logo-line"><label class="label">上傳頭像:</label><input type="text" name="logo" id="logo" class="txtinput" /><input type="button" value="瀏覽" onClick="upFile()"><span class="logo"><img src="__PUBLIC__/Admin/images/getAvatar.jpg" id="image" /></span></p> <p><label class="label">暱稱:</label><input type="text" name="" class="txtinput" /></p> <p><label class="label">性別:</label><label><input type="radio" name="gender" value="0">男</label> <label><input type="radio" name="gender" value="1">女</label></p> </div> </div> <form id="fileUpload" name="fileUpload" method="post" enctype="multipart/form-data" target="frameFile"> <input id="upfile" name="upfile" contenteditable="false" type="file" style="display:none;" onChange="$('#fileUpload').attr('action','__URL__/frameUpload?' + new Date().getTime() );document.fileUpload.submit();" /> </form> <!-- 實現本頁面圖片顯示 --> <iframe id="frameFile" name="frameFile" style="display: none;"></iframe>
<script type="text/javascript"> function upFile(){ var isIE=document.all && window.external; if(!isIE){ document.getElementById('upfile').click(); } } function InsertMsgPic($state,imgthumb) { if($state=='SUCCESS'){ $('#image').attr('src',imgthumb); $('#logo').val(imgthumb); }else{ alert($state); } } </script>
//框架上傳 public function frameUpload(){ //配置上傳參數 $config = array( "rootPath"=>'./Uploads/', "savePath" => $this->savePath, "maxSize" => 20000000, // 單位B "exts" => explode(",", 'gif,png,jpg,jpeg,bmp'), "subName" => $this->sub_name, ); $upload = new \Think\Upload($config);// 實例化上傳類 $info = $upload->upload(); if($info) { //上傳成功 獲取上傳文件信息 $state = "SUCCESS"; }else{ // 上傳錯誤提示錯誤信息 $state = "ERROR" . $upload->getError(); } $file_url = '/Uploads/'.$info['upfile']['savepath'].$info['upfile']['savename']; header('Content-type: text/html; charset=UTF-8'); echo '<script>window.parent.InsertMsgPic("'.$state.'","'.$file_url.'");</script>'; exit; }