thinkphp5单图上传即时显示

//前端html部分
 <!DOCTYPE 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;
 }

猜你喜欢

转载自blog.csdn.net/luoangen/article/details/82801927