用FormData实现无刷新页面异步上传文件

前端javascript:

 $('#upfile').change(function(e){

        var file = this.files[0];
        var type = file.type.split('/')[1];

        if(type != 'jpg' && type != 'jpeg' && type != 'png' && type != 'gif'){
            $(this).parent('form')[0].reset();
            errmsg('上传头像的格式必须为jpg,png或gif!');
            return;
        }
        if(file.size > 2097152){
            $(this).parent('form')[0].reset();
            errmsg('头像大小不得超过2M,请重新上传');
            return;
        }

        var formData = new FormData();
        formData.append('file', this.files[0]);


        var $lay, $index;
        $.ajax({
            url: "upload",
            type: "POST",
            dataType : 'JSON',
            data: formData,
            processData: false,  // 告诉jQuery不要去处理发送的数据
            contentType: false,   // 告诉jQuery不要去设置Content-Type请求头
            beforeSend : function(){
                layer.msg("正在上传...", {
                    icon : 16,
                    time : 0,
                    shade : 0.2,
                    success : function(lay, index){
                        $lay = lay;
                        $index = index;
                    }
                });
            },

            success : function(resp){
                setTimeout(function(){
                    var html = '<i class="layui-layer-ico layui-layer-ico'+(resp.errcode == 0 ? 1 : 2)+'"></i>'+resp.errmsg;
                    $lay.children('.layui-layer-content').html(html);
                    setTimeout(function(){
                        layer.close($index);
                    }, 1500);
                }, 1000);
                if(resp.errcode == 0){
                    $('#face').attr('src', resp.logo);
                }
            }
        });


php处理的upload方法:

public function actionUpload(){

        if(!Yii::$app->getSession()->has('user_id')){
            JsonHelper::ajaxSend(1, '请先登录!');
        }
        $uid = 0;
        if(self::existSession('user_id')){
            $uid = self::getSession('user_id');
        }else if(self::existCookie('user_id')){
            $uid = self::getCookie('user_id');
        }

        $res = NewsUsers::find()->where(['user_id' =>$uid])->one();
        if(empty($res)){
            JsonHelper::ajaxSend(2, '用户不存在!');
        }



        if(empty($_FILES['file'])){
            JsonHelper::ajaxSend(3, '上传头像不能为空');
        }
        if($_FILES['file']['error'] != 0){
            JsonHelper::ajaxSend(4, '上传图片出错!请重新上传!');
        }
        if($_FILES['file']['size'] == 0){
            JsonHelper::ajaxSend(5, '上传图片大小为0');
        }

        if($_FILES['file']['size'] > 2097152){
            JsonHelper::ajaxSend(6, '头像大小不得超过2M,请重新上传');
        }

       
        $image = $_FILES['file'];
        $type = FileHelper::getFileRealType($image['tmp_name']);
        if(!in_array($type, ['jpg', 'jpeg', 'png', 'gif'])){
            JsonHelper::ajaxSend(7, '上传头像格式必须为jpg、png或gif!');
        }

        $info = BaseUserHelper::createProfile($image, $res['logo']);

        if($info['errcode'] != 0){
            JsonHelper::ajaxSend(8, $info['errmsg']);
        }

        $res->logo = $info['logo'];

        if(!$res->save(false)){
            JsonHelper::ajaxSend(9, '上传头像失败');
        }

        $result = JsonHelper::setErrMsg(0, '上传头像成功');
        $result['logo'] = UserHelper::getUserLogo($info['logo']);
        JsonHelper::ajaxSend($result);


    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值