美文网首页
thinkphp结合ajax上传图片 转载

thinkphp结合ajax上传图片 转载

作者: geeooooz | 来源:发表于2020-06-16 16:44 被阅读0次

https://www.jianshu.com/p/806eb93a704c

1.上传表单

<form action="" id="scid" enctype="multipart/form-data" method="post" >
<input type="text" name="name" />
<input type="file" name="photo" />
<input type="submit" value="提交" >
</form>

注意  form 一定要加 enctype="multipart/form-data"

2.前台JS

//formAdd为from表单ID
var formElement = document.querySelector("#scid");
var formdata = new FormData(formElement);
$.ajax({
     url:"{:url('info/add')}",
     type:'POST',
     data:formdata,
     async:false,
     processData:false,
    contentType:false,
    success:function (res) {
        // console.log(res);
        // return ;
        if (res.code==1){
            //toastr.success(res.msg, '正在跳转到列表页面...');
            window.location.href=res.url;
        } else{
            //$('#divInfo').toggleClass('sk-loading');
           // toastr.error( res.msg);
           alert(res.msg);
        }
    }
});

3.后台代码

public function transferhold(){
        $info = $this->_swfupload();
        dump($info);
}
protected function _swfupload() {
        if (IS_POST) {
            //上传处理类
            $config=array(
                    'rootPath' => './'.C("UPLOADPATH"),
                    'savePath' => 'enterprise/',
                    'maxSize' => 110485760,
                    'saveName'   =>    array('uniqid',''),
                    'exts'       =>    array('jpg', 'gif', 'png', 'jpeg',"mp4","wmv"),
                    'autoSub'    =>    true,
            );
            $upload = new \Think\Upload($config);
            $info=$upload->upload();
            return $info;
        }
        return false;
}

相关文章

网友评论

      本文标题:thinkphp结合ajax上传图片 转载

      本文链接:https://www.haomeiwen.com/subject/tderxktx.html