thinkphp5單圖上傳即時顯示

//前端html部分

 <html lang="en">
     <head>
          <meta charset="UTF-8">
          <title>單圖片上傳</title>
          //引入js
          <script src="xxxxxx/js/jquery1.42.min.js"></script>
    </head>
    <body>
         <img onclick=" $('#weblogo_file').click();" id="img1" src="/uploads/tupian/0.png"/>
         <iframe name="ifr" frameborder="0"></iframe>
         <form style="display: none" target="ifr" action="/index.php/index/index/upload" method="post" enctype="multipart/form-data">
             <input type="text"name="id" value="5"><br/>
             <input id="weblogo_file" type="file" onchange=" $('#weblogo_submit').click();" name="pic"><br/>
             <input id="weblogo_submit" type="submit"  value="上傳">
         </form>
     </body>
 </html>

//渲染頁面

function uploadtest(){
  return $this->fetch();
}

//上傳顯示方法

function upload(){
    $res = request()->param();
    $id = $res['id'];
    $file = request()->file('pic');
    $pic = $id.'.png';
    $file->move(ROOT_PATH . 'public' . DS . 'uploads/tupian',$pic);
    echo '<script>
          window.parent.document.getElementById(\'img1\').src = "/uploads/tupian/'.$pic.'?" + Math.random()
         </script>';
    exit;
 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章