温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

tp3.2和mbUploadify.js上传图片实例

发布时间:2020-05-07 15:33:08 来源:亿速云 阅读:628 作者:Leah 栏目:编程语言

这篇文章主要为大家详细介绍了tp3.2和mbUploadify.js上传图片的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

HTML:

<p class="form-group">
    <label class="col-sm-1 control-label no-padding-right" for="form-field-4"> 图片: </label>
    <p class="col-sm-9">
        <input type="file" name="files" id="imgfile" multiple style="display:none;" onchange = "imgpath.value=this.value" >
        <input type="textfield" id="imgpath" style="border: 0px;outline:none;cursor: pointer;width:100px;display:none;">
        <input type="button" class="btn btn-white btn-info btn-sm" value="点击上传图片" onclick="imgfile.click()">
        <p class="space-4"></p>
        <p id="img-data" class="img-data">
            <if condition="$data['savepath'] neq ''">
                <span class="uploadimg">
                    <img src="{$data['savepath']}" style="max-width: 300px;">
                    <input type="hidden" name="img" value="{$data['img']}">
                    <a class="remove-uploadimg" title="删除">✕</a>
                </span>
            </if>
        </p>
        <p class="space-4"></p>
        <p id="imgError" class="msg"></p>
    </p></p>

CSS:

<style>
    .remove-uploadimg{ cursor:pointer;}
    .uploadimg{
        display: inline-block;
        position: relative;
    }
    .uploadimg .remove-uploadimg{
        position: absolute;
        font-size: 20px;
        top:-10px;
        right: -6px;
    }
    .remove-uploadimg{
        width:30px;
        height:30px;
        background-color:#ccc;
        border-radius:50%;
        color:red;
        text-align:center;
    }
    .remove-uploadimg:hover{
        text-decoration: none;
    }</style>

JS:

<script src="__PUBLIC__/js/mbUploadify.js"></script>
<script>    var upload1 = new mbUploadify({
        file: document.getElementById('imgfile'),        /*ajax 上传地址*/
        url: "{:U('Upload/mbUploadImg')}",        //上传进度
        progress: function(){
            $('#imgpath').show();
            $('#imgpath').val('上传中...');
        },        /*上传失败*/
        error: function(file, msg){
            document.getElementById('imgError').innerHTML = msg;
        },        /*ajax上传成功*/
        uploadSuccess: function(res){
            $('#imgpath').hide();
            $('#imgpath').val('');            var data = JSON.parse(res);
            document.getElementById('img-data').innerHTML = '<span class="uploadimg">' +
                    '<img src="'+ data.savepath +'" style="max-width: 300px;">' +
                    '<input type="hidden" name="img" value="'+data.id+'">'+
                    '<a class="remove-uploadimg" title="删除">✕</a>' +
                    '</span>';
        }
    });
    $('body').on('click','.remove-uploadimg',function(){
        $(this).parents('.uploadimg').remove();
    })</script>

PHP:

public function mbUploadImg(){    $upload = new Upload(); // 实例化上传类
    $upload->maxSize = 5242880 ; // 设置附件上传大小
    $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
    $upload->rootPath =  './Public/';    // 上传文件
    $info = $upload->uploadOne($_FILES['files']);    if($info) {        // 上传成功
        $data['name'] = $info['name'];        $data['ext'] = $info['ext'];        $data['type'] = $info['type'];        $data['savename'] = $info['savename'];        $data['savepath'] = "/Public/".$info['savepath'].$info['savename'];        $imgId = M('upload_img')->add($data);        if($imgId){            $resData['code'] = 200;            $resData['msg'] = '成功';            $resData['id'] = $imgId;            $resData['name'] = $data['name'];            $resData['savepath'] = $data['savepath'];            echo json_encode($resData);            return;
        }
    }    // 上传错误提示错误信息
    return $this->ajaxReturn(['code'=>400,'msg'=>$upload->getError()]);
}

关于tp3.2和mbUploadify.js上传图片的方法就分享到这里了,当然并不止以上和大家分析的办法,不过小编可以保证其准确性是绝对没问题的。希望以上内容可以对大家有一定的参考价值,可以学以致用。如果喜欢本篇文章,不妨把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI