THINKPHP_Jquery.form_单图片上传

HTML文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
 <title>Document</title>
 <link rel="stylesheet" type="text/css" href="__PUBLIC__/Style/weireset.css" />
 <script type="text/javascript" src="__PUBLIC__/Style/jquery172.js"></script>
 <script type="text/javascript" src="__PUBLIC__/Style/jquery.form.js"></script>
</head>
<body>
 <script type="text/javascript">
 $(function(){
  $("#t_file").change(function(){
      if($("#t_file").val() == "") return;
   $("#t_form").ajaxSubmit({
    dataType:  'json',
    beforeSend: function() {
           $('#t_stat').empty();
     $('#t_stat').html("上传中...");
       },
    success: function(data) {
     if(data.state == 'SUCCESS'){    
      $('#t_stat').html(""+ data.title +" 上传成功("+data.size+"k)");
      var timg = data.turl;//原图
      $('#t_img').attr("src",timg);
      $("#t_dir").val(timg);
     }else {
      $('#t_dir').html(data.state);  
     }   
    },
    error:function(xhr){
     $('#t_stat').html("上传数百");
     $('#t_dir').html(xhr);
    }
   });
     });
 });
 </script>
 <div class="bordered" style="margin:100px;padding:10px;">
 <form id="upload1" action="{:U('Manage/Test/submit')}" method="post">
  <input type="text" name="title" value="这里是标题" /><br/>
  <form id='t_form' action='{:U('Manage/Test/ajaxUpload')}' method='post' enctype='multipart/form-data'>
   <input id="t_file" type="file" name="imgfile" class="mtp10" /><br/>
</form>
  <input id="t_dir" type="text" name="thumb" size="60" value="" /><br/>
  <span id="t_stat">上传状态</span><br/>
  <input type="submit" value="SUBMIT" class="mtp10" /><br/>
 </form>
 <img id="t_img" src="/xyh/Uploads/Thumbs/20140701/53b250c77f0ac.jpg" alt="111" width="240" height="120" class="mtp10" />
 </div>
</body>
</html>

PHP文件

    public function ajaxUpload(){
        header("Content-Type:text/html; charset=utf-8");
        $config=C('uploadImg');
        $upload=new \Think\Upload($config);
        $info=$upload->uploadOne($_FILES['imgfile']);
        if(!$info){
            $this->error($upload->getError());
        }else{
            
            $info['savepath'] = __ROOT__.ltrim($info['savepath'],'.');
            $picture_turl=$info['savepath'].$info['savename'];
            echo json_encode(
                array(
                'url' => $picture_url,
                'turl' => $picture_turl,
                'title' => $info['name'],
                'original' => $info['name'],
                'state' => 'SUCCESS',
                'size' => round($info['size']/1024,2)
            ));
   
        }
    }

PS:适用于THINKPHP3.2.2

转载于:https://my.oschina.net/miaowang/blog/285920

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值