ThinKPHP框架+ajax实现图片无刷新上传

ThinKPHP框架+ajax实现图片无刷新上传

本程序是结合视频教程和百度资料整理出的,以便以后学习和使用:


  1. 前端文件
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>无刷新上传</title>
    <script src="__PUBLIC__/up/jquery.min.js" type="text/javascript"></script>
    <script src="__PUBLIC__/up/jquery.uploadify.min.js"></script>
    <link rel="stylesheet" type="text/css" href="__PUBLIC__/up/uploadify.css">

</head>
<body>
    <form action="{:U('inserts')}" method="post" enctype="multipart/form-data">
        <p>IMG</p>
        <div id="imgs"></div>
        <input id="ups" name="img" type="file">

        <input type="hidden" name="img" id="img_post">
        <input type="submit" name="" value="提交">
    </form>


</body>
<script type="text/javascript">
    var img = '';
    $('#ups').uploadify({
        'swf'        : '__PUBLIC__/up/uploadify.swf',
        'uploader'   : '{:U("User/ajax_up")}',
        'buttonText' : '缩略图上传',
        onUploadSuccess : function(file, data, response) {
            //这个数据返回有点问题,所以这个函数截取的是图片的路径和名字
            $("#img_post").val(data.substring(0,28));
            img += "<img width='200px' src='/tp2/Uploads/"+data.substring(0,28)+"'>";
            $('#imgs').html(img);
        }

    });
</script>

</html>

2.服务器文件

//模板文件
public function adds(){
    $this -> display();
    }

//上传类
public function ajax_up(){
    if(!empty($_FILES)) {
        $upload = new \Think\Upload();
        $images = $upload -> upload();
        //判断是否有图
        if($images){
            $info = $images['Filedata']['savepath'].$images['Filedata']['savename'];
            echo $info;
        }else{
            $this -> error($upload -> getError());
        }
    }
}

//把图片信息保存到数据库
public function inserts(){
    //没有具体去实现
    dump($_POST);
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值