tp5 上传图片的实现

<td>网站logo:</td>
                                <td>
                                    <img src="{$data.web_logo?'/'.$data.web_logo:'/static/Admin/assets/img/add_photo.png'}" alt=""
                                         style="margin-left:20px;border:1px solid #ccc;" width="100px;" height="100px;"
                                         onclick="get_img(this)" id="web_logo">
                                    <input type="file" class="form-control" id="qrcode"
                                           style="display: none;padding-left:20px;" onchange="showimg(this)">
                                    <span style="color: red;margin-left: 10px;">建议尺寸:240*200</span>
                                </td>

 js代码:

var formData = new FormData();
    function showimg(e) {
        var file = e.files[0];
        var type = file.type;
        if (!type.match(/.jpg|.jpeg|.png|.bmp/i)) {  //判断上传文件格式
            $(e).val('');
            return swal('温馨提示', '上传的图片格式不正确,请重新选择');
        }
        formData.set($(e).attr('id'), file);
        var img = $(e).prev().get(0);
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function () {
            img.src = reader.result;
        };
        $(e).val('');
    }

    function get_img(obj) {
        $(obj).next().click();
    }

    $('#btn-add').on('click', function () {      

        $.ajax({
            url:'',
            type:'POST',
            data:formData,
            contentType: false,
            processData: false,
            dataType:'json',
            success:function (data) {
                 console.log(data);
                
            }
        })

    });

php代码:

        $pic = \Request::file('qrcode');
            if ($pic) {
                $info_file = $pic->move('./uploads');
                if ($info_file) {
                    $data['web_logo'] = 'uploads/' . $info_file->getSaveName();
                }
            }

完成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值