thinkphp 下多圖ajax上傳圖片

來源:互聯網
上載者:User
這篇文章主要介紹了關於thinkphp 下多圖ajax上傳圖片,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

碰到一個項目,有一個比較繁瑣的功能6個ajax上傳,基本上每個上傳邏輯多不一樣,記錄一下

thinkphp的view頁面:

id方便找到這個元素 name一定要加 [ ]

<div class="btns">  <a href="javascript:;" class="a-upload">    <input type="file" id="fileaq" name="fileaq[]" data-filesType="words" class="uploadInput" multiple="multiple" />    <i class="iconfont icon-shangchuan"></i>上傳附件  </a>  <a href="javascript:void(0)" class="submit" id="aq_sub">發布</a></div>

點擊發布先判定一下,然後在把需要的參數傳遞給doUploadFiles函數

    //發布案情        $('#aq_sub').click(function() {                    var guanxi = 'many_one';                    var type_file = 'file';                    var type_name = 'fileaq';                    var anqing = $('#anqing').val();                    if ($.trim(anqing).length == 0) {                layer.alert('請輸入內容!\n');                $('#anqing').focus();                                return false;            } else {                            var cate_id = 3;                doUploadFiles(cate_id, type_file, type_name, guanxi, anqing);            }        })

參數說明

cate_id:多個上傳的識別id

type_file: 判斷是圖片還是檔案上傳(nofiley:有的檔案不需要上傳)

type_name:上傳檔案的id

guanxi:關係 項目需要 參數分為many_one,many_many,one_one(多個使用者每人一條資料,多個使用者每人多條資料,多個使用者一人一條資料)

content:內容

function doUploadFiles(cate_id, type_file, type_name, guanxi, content) {        var guanxi = arguments[3] ? arguments[3] : 'many_one'; //設定關係        var formData = new FormData();                var fangchan_id = $('#fangchan_id').val();        formData.append("fangchan_id", fangchan_id);        formData.append("cate_id", cate_id);        formData.append("guanxi", guanxi);        formData.append("content", content);                if(type_file !='nofile'){            formData.append("type_file", type_file);            formData.append("file_length", $("#"+type_name)[0].files.length);                        for(var i=0; i<$("#"+type_name)[0].files.length;i++){                formData.append('file[]',$("#"+type_name)[0].files[i]);            }        }        $.ajax({            url: '/Property/jindiaoHandle',            type: 'POST',            data: formData,            dataType: "json",            async: false,            cache: false,            contentType: false,            processData: false,            success: function(data) {                console.log('上傳:',data)                                if (data.status == 200) {                    layer.msg(data.msg, { icon: 1 });                    window.location.reload();                } else {                    layer.msg(data.msg, { icon: 1 });                                        return false;                }            }        });    }

php代碼比較長

/**     * 提交房源盡調     */    public function jindiaoHandle()    {        $user_id = session('user_id');        $fangchan_id = I('post.fangchan_id');        $cate_id = I('post.cate_id');        $cate_arr = array('6','7','8');        $content = I('post.content');        $type_file = I('post.type_file');        $file_length = I('post.file_length');   //判斷是否上傳檔案        //many_one  多個使用者存在一條   many_many 多個使用者存在多條    one_one 只能催在一條資料        $guanxi = I('post.guanxi');        $guanxi?$guanxi:'many_one';        $content?$content:'0';        if(empty($user_id)){            $ret = ['status' => '1001', 'msg' => '請先登入!'.$user_id, 'data' => ''];            $this->ajaxReturn($ret, 'json');        }else{            $level = M('users')->where(['user_id' => $user_id])->getField('level');            //判斷是不是法拍經理            if ($level != 2) {                $ret = ['status' => '1002', 'msg' => '您沒有許可權填寫!', 'data' => ''];                $this->ajaxReturn($ret);            }        }        if(empty($fangchan_id))        {            $ret = ['status' => '1003', 'msg' => '找不到此房源!', 'data' => ''];            $this->ajaxReturn($ret);        }        if(empty($cate_id))        {            $ret = ['status' => '1004', 'msg' => '找不到此盡調類型!', 'data' => ''];            $this->ajaxReturn($ret);        }        if(empty($content))        {            $ret = ['status' => '1005', 'msg' => '內容不可為空!', 'data' => ''];            $this->ajaxReturn($ret);        }        $fc_user_id = M('fangchan')->where(['fangchan_id' => $fangchan_id])->getField('user_id');        //判斷是不是該房產的法拍經理        if ($fc_user_id == $user_id) {            $data = [                'fangchan_id' => $fangchan_id,                'user_id'     => $user_id,                'cate_id'     => $cate_id,                'content'     => $content,                'res_num'     => $file_length,                'add_time'    => time(),                'is_user'     => 1,                'is_show'     => '1',            ];        } else {            if(!in_array($cate_id,$cate_arr))            {                $fc_add_time = M('fangchan')->where(['fangchan_id' => $fangchan_id])->getField('add_time'); //擷取添加時間                if ((time() - $fc_add_time) < (12 * 60 * 60)) {                    $arr = ['status' => '1006', 'msg' => '請於24小時候後來發布!', 'data' => ''];                    $this->ajaxReturn($arr, 'json');                }            }            $data = [                'fangchan_id' => $fangchan_id,                'user_id'     => $user_id,                'cate_id'     => $cate_id,                'content'     => $content,                'res_num'     => $file_length,                'add_time'    => time(),                'is_user'    => 0,                'is_show'     => '1',            ];        }        if($guanxi=='many_one')        {            $fc_jindiao_data = M('fangchan_jindiao')                ->where(['fangchan_id'=>$fangchan_id,'user_id'=>$user_id,'cate_id'=>$cate_id])                ->getField('jindiao_id');            //判斷房產盡調是修改還是添加            if($fc_jindiao_data){                $res_edit = M('fangchan_jindiao')->where('jindiao_id='.$fc_jindiao_data)->save($data);            }else{                $res_add = M('fangchan_jindiao')->add($data);            }        }elseif($guanxi=='one_one')        {            $fc_jindiao_data = M('fangchan_jindiao')                ->where(['fangchan_id'=>$fangchan_id,'cate_id'=>$cate_id])                ->getField('jindiao_id');            //判斷房產盡調是修改還是添加            if($fc_jindiao_data){                $res_edit = M('fangchan_jindiao')->where('jindiao_id='.$fc_jindiao_data)->save($data);            }else{                $res_add = M('fangchan_jindiao')->add($data);            }        }elseif($guanxi=='many_many')        {            $res_add = M('fangchan_jindiao')->add($data);        }        //判斷是否有檔案        if(!empty($file_length) || $file_length!=0)        {            $result = self::uploadFile($type_file);            if($result['status'] == -1){                exit(json_encode(array("status"=>-1,"msg"=>$result['msg'],'result'=>'')));            }            $add_time = time();            if(!empty($res_edit))            {                $where=array('user_id' => $user_id,'fangchan_id' => $fangchan_id,'cate_id'=> $cate_id);                $r_info = M('fangchan_jindiao')->where($where)                    ->getField('jindiao_id');                if($r_info)                {                    foreach ($result['result'] as $v)                    {                        if($type_file=='file')                        {                            $data=array('user_id' => $user_id,'fangchan_id' => $fangchan_id,'cate_id'=> $cate_id,'add_time'=> $add_time,'jindiao_id'=> $r_info,'file'=>$v);                            $ziyuan_info = M("fangchan_jdresources")->data($data)->add();                        }elseif($type_file=='image')                        {                            $data=array('user_id' => $user_id,'fangchan_id' => $fangchan_id,'cate_id'=> $cate_id,'add_time'=> $add_time,'jindiao_id'=> $r_info,'images'=>$v);                            $ziyuan_info = M("fangchan_jdresources")->data($data)->add();                        }                    }                    if($ziyuan_info)                    {                        $ret =[                            'status'=>200,                            'msg'=>'上傳成功',                            'data'=> $data                        ];                    }else{                        $ret =[                            'status'=>1009,                            'msg'=>'上傳資源失敗',                            'data'=> ''                        ];                    }                }else{                    $ret =[                        'status'=>1008,                        'msg'=>'上傳資源失敗',                        'data'=> ''                    ];                }            }elseif(!empty($res_add))            {                foreach ($result['result'] as $v)                {                    if($type_file=='file')                    {                        $data=array('user_id' => $user_id,'fangchan_id' => $fangchan_id,'cate_id'=> $cate_id,'add_time'=> $add_time,'jindiao_id'=> $res_add,'file'=>$v);                        $ziyuan_info = M("fangchan_jdresources")->data($data)->add();                    }elseif($type_file=='image')                    {                        $data=array('user_id' => $user_id,'fangchan_id' => $fangchan_id,'cate_id'=> $cate_id,'add_time'=> $add_time,'jindiao_id'=> $res_add,'images'=>$v);                        $ziyuan_info = M("fangchan_jdresources")->data($data)->add();                    }                }                if($ziyuan_info)                {                    $ret =[                        'status'=>200,                        'msg'=>'上傳成功',                        'data'=> $data                    ];                }else{                    $ret =[                        'status'=>1010,                        'msg'=>'上傳資源失敗',                        'data'=> ''                    ];                }            }else{                $ret =[                    'status'=>1011,                    'msg'=>'上傳資源失敗',                    'data'=> ''                ];            }        }elseif(empty($res_add) && empty($res_edit)){            $ret =[                'status'=>1007,                'msg'=>'上傳失敗',                'data'=> ''            ];        }else{            $ret =[                'status'=>200,                'msg'=>'上傳成功',                'data'=> ''            ];        }       $this->ajaxReturn($ret);    }    /*    *多圖上傳     */    public function uploadFile($type='file'){        if($type=='file')        {            $type_info = array('doc', 'docx', 'xls', 'xlsx','zip','rar');            $type_path = '/Public/upload/jidiao/files/';        }elseif($type=='image'){            $type_info = array('jpg', 'gif', 'png', 'jpeg');            $type_path = '/Public/upload/jidiao/images/';        }        $upload = new \Think\Upload();// 執行個體化上傳類        $upload->maxSize   =     1 * 1024 * 1024;// 設定附件上傳大小        $upload->exts      =     $type_info;// 設定附件上傳類型        $upload->rootPath  =      '.'.$type_path; // 設定附件上傳根目錄        $upload->savePath  =      ''; // 設定附件上傳(子)目錄        $upload->subName   = array('date','Y/m-d');        //上傳檔案        $info = $upload->upload();        $picurl = array();        if(!$info) {// 上傳錯誤提示錯誤資訊            return array('status'=>-1,'msg'=>$upload->getError(),'result'=>'');        }else{// 上傳成功 擷取上傳檔案資訊            foreach($info as $file){                $picurl[] = $type_path.$file['savepath'].$file['savename'];            }            return array("status"=>1,"msg"=>'上傳成功','result'=>$picurl);        }    }

以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.