设为首页 加入收藏

TOP

使用tp3.2和mbUploadify.js上传图片的代码,记录一下(一)
2019-08-23 00:41:29 】 浏览:46
Tags:使用 tp3.2 mbUploadify.js 上传 图片 代码 记录

HTML:

<div class="form-group">
    <label class="col-sm-1 control-label no-padding-right" for="form-field-4"> 图片: </label>
    <div 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()">
        <div class="space-4"></div>
        <div 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>
        </div>
        <div class="space-4"></div>
        <div id="imgError" class="msg"></div>
    </div>
</div>

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
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇php柱状图多系列动态实现 下一篇第五课:PHP echo和print 语句

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目