zoukankan      html  css  js  c++  java
  • ajax表单上传图片和数据

    js

        $('#addbtn').click(function () {
                var form = document.getElementById("img-form");//获取表单的数据
                var formdata = new FormData(form);//格式化表单数据
                console.log(formdata);
                $.ajax({
                    type: "POST",
                    async:false,//同步请求
                    processData: false,// 不处理发送的数据
                    contentType: false,// 不设置Content-Type请求头
                    dataType: "json",
                    url: "{:url('admin/banner/upload')}",
                    data: formdata,
                    success: function (res) {
                        if (res.code == 1) {
                            layer.msg(res.msg, {
                                icon: 6,
                                time: 2000
                            }, function () {
                                window.location.href = res.url;
                            })
                        } else {
                            layer.open({
                                title: '添加失败',
                                content: res.msg,
                                icon: 5,
                                anim: 6
                            })
                        }
                    }
                });
            })

    控制器

        // 上传图片
        public function upload()
        {
            if (request()->isPost()) {
                $data = [
                    'title' => input('title'),
                    'desc' => input('desc'),
                    'link' => input('link')
                ];
                $file = request()->file('image');
                if (!$file) {
                    $this->error($file->getError());
                }
                //验证
                $map = [
                    'ext' => 'jpg,png,bmp,jpeg,gif',
                    'size' => '3000000' //3MB 文件大小单位是字节
                ];
                $info = $file->validate($map)->move(ROOT_PATH . 'public' . DS . 'uploads');
                if (!$info) {
                    $this->error($file->getError());
                }
                $data['path'] = DS . 'uploads' . DS . $info->getSaveName();
                $res = model('Banner')->upload($data);
                if ($res == 1) {
                    $this->success('添加成功!', 'admin/banner/bannerlist');
                } else {
                    $this->error($res);
                }
            }
        }

    模型

    class Banner extends Model
    {
        //文件上传
        public function upload($data)
        {
            $validate = new ValidateBanner();
            if (!$validate->scene('upload')->check($data)) {
                return $validate->getError();
            }
            $res = $this->allowField(true)->save($data);
            if ($res) {
                return 1;
            } else {
                return '上传失败!';
            }
        }
    }

    验证器

    class Banner extends Validate
    {
        protected $rule = [
            'title|标题' => 'require',
            'link|链接' => 'require',
            'desc|描述' => 'require'
        ];
        protected $scene = [
            'upload' => ['title', 'link', 'desc']
        ];
    }
    ╰︶﹉⋛⋋⊱⋋๑๑⋌⊰⋌⋚﹉︶╯
  • 相关阅读:
    数据结构-二叉树--二叉搜索树
    Django之Model操作
    Django之Form组件
    Django之url(路由)配置
    Django框架之模板继承和静态文件配置
    Django框架之第三篇模板语法(重要!!!)
    Django ORM操作及进阶
    django 之 ORM操作多表联查总结
    人生苦短,我学python之python xml数据解析
    人生苦短,我学python之python re正则表达式
  • 原文地址:https://www.cnblogs.com/zhangcheng001/p/12147044.html
Copyright © 2011-2022 走看看