layui 单图片上传 多图片批量上传

html 代码

<div class="row umar-t16 line-32">
                            <div class="col-md-1 col-sm-1 col-xs-1 tx-r vtc-c" style="line-height: 60px;height: 60px;">主图片:</div>
                            <div class="col-md-2 col-sm-2 col-xs-2 vtc-c" style="line-height: 60px;">
                                <div class="input-group input-group-sm ub-fh">
                                    <input type="hidden" name="img" id="img" value="${map.img}">
                                    <button type="button" id="headImg" class="btn btn-primary" >上传</button>
                                    <span class="rela in-show  umar-l  uinn-b hide" style="width: 60px;height: 60px;" id="iconspan" >
                                        <img class="ub-fv ub-fh" id="uploadHead" onclick="previewImg(this)">
                                        <i class="iconfont icon-close_black removeImage" onclick="removeImage(this)" style="border-radius: 10px;position: absolute;top: 4px;right: 0;background: red;font-size:  12px;color: #ffffff;width: 20px;height: 20px;text-align: center;line-height: 20px;">
                                        </i>
                                    </span>
                                </div>
                            </div>
                            <div class="col-md-1 col-sm-1 col-xs-1 tx-r" style="line-height: 60px;height: 60px;">批量图片:</div>
                            <div class="col-md-7 col-sm-7 col-xs-7 vtc-c" style="line-height: 60px;">
                                <div class="input-group input-group-sm ub-fh imagesdiv">
                                    <input type="hidden" name="maps[imgs]" id="imgs" value="${map.imgs}"/>
                                    <button type="button" id="headImgs" class="btn btn-primary" >上传</button>
                                </div>
                            </div>
                        </div>

js  代码

var imgs = "";
var type = "${param.type==null?map.type:param.type}";
$(function(){
    var img="${map.img}";  //修改 查询出来的 主图片
    imgs="${map.imgs}"; ////修改 查询出来的 批量图片
    //$("#uploadHead").attr("src","<%=basePath%>"+icons);
    if($.funcUtils.isNotEmpty(img))
        showImgs(img,"main");
    if($.funcUtils.isNotEmpty(imgs))
    $.each(imgs.split(","),function(i,src){
        if(i<imgs.split(",").length-1)
            showImgs(src,"other");
    })

})
//上传一张主图片
layui.use(['upload', 'element'], function() {
    var upload = layui.upload,
    element = layui.element; //上传
    upload.render({
        elem: '#headImg',
        url: '<%=basePath%>/uploadingFile/uploadFile.html?folderName=UserPicture', //UserPicture上传到本地磁盘resin4.0文件夹 
        field:'Filedata',
        xhr: xhrOnProgress,
        accept: "images", //普通文件
        exts: 'jpg|png|jpeg|gif',//设置可上传文件
        multiple: false, //多文件上传
        done: function(res, index, upload) { //每个文件提交一次触发一次。详见“请求成功的回调”
            showImgs(res.fileInfo,"main");
        }
    });
});
function previewImg(_this) { //预览主图片
    var src = $(_this).attr("src");// style='max-height:600px;max-width:100%;'
    var imgHtml = "<div class='tx-c'><img src='" + src + "' /></div>"; 
    //弹出层 
    windowTop.$sys.layer.open({ 
        maxmin: true,
        type: 1, 
        shade: 0.8,
        offset: 'auto', 
        area: ['500px','400px'],
        closeBtn:1,
        skin:'layui-layer-nobg',
        shadeClose:true,//点击外围关闭弹窗 
        scrollbar: false,//不现实滚动条 
        title: "图片预览", //不显示标题  
        content: imgHtml //捕获的元素,注意:最好该指定的元素要存放在body最外层,否则可能被其它的相对元素所影响  
    });
}


//批量多张图片上传
layui.use(['upload', 'element'], function() {
    var upload = layui.upload,
    element = layui.element; //上传
    upload.render({
        elem: '#headImgs',
        url: '<%=basePath%>/uploadingFile/uploadFile.html?folderName=UserPicture',
        field:'Filedata',
        xhr: xhrOnProgress,
        accept: "images", //普通文件
        exts: 'jpg|png|jpeg|gif',//设置可上传文件
        multiple: true, //多文件上传
        size:10240,//图片大小
        number:30,//上传的数量
        bindAction: '#test9',
        before: function(obj) {
            /* //预读本地文件示例,不支持ie8
            obj.preview(function(index, file, result) {
                $('#demo').append('<img src="' + result 
                    + '" alt="' + file.name 
                    +'"height="92px" width="92px" class="layui-upload-img uploadImgPreView">');
            }); */
        },
        done: function(res, index, upload) { //每个文件提交一次触发一次。详见“请求成功的回调”
            //每个图片上传结束的回调,成功的话,就把新图片的名字保存起来,作为数据提交
            if(res.code!="1"){
                imgs=imgs+res.fileInfo+",";
                showImgs(res.fileInfo,"other");
            }
        }
    });
});
//显示图片
function showImgs(src,type){
    if(type=="main"){
        $("#img").val(src);
        $("#iconspan").removeClass("hide");
        $("#iconspan img").attr("src",src);
    }else{
        var $icons = $("#iconspan").clone();
        $icons.removeClass("hide");
        $icons.attr("id","");
        $icons.find("img").attr("src",src);
        $(".imagesdiv").append($icons);
    }
}
var editor = null;
function editorInit(){//ck插件
    editor =CKEDITOR.replace('editor',{height:"100px;",toolbar_Full:"Basic"});
    var html = $("#content").val();
    editor.setData(html);//把数据保存在ck插件里
}
//点击X删除图片
function removeImage(_this){
    var pid = $(_this).parent().attr("id");
    if(pid=="iconspan"){
        $(_this).parent().addClass("hide");
        $("#img").val("");
        $("#iconspan img").attr("src","");
    }
    else{
        var src = $(_this).prev().attr("src")+",";
        imgs = imgs.replace(src, "");
        $(_this).parent().remove();
    }
}

java 图片上传到本地磁盘 resin4.0文件里面    返回图片路径到 input框img    在通过添加方法到数据库就可以了

java后台

    /***
     * 个人文件上传
     * 
     * @param Filedata
     * @param request
     */
    @RequestMapping("uploadFile")
    @ResponseBody
    public synchronized Object uploadFile(@RequestParam("Filedata") MultipartFile Filedata, HttpServletRequest request,HttpServletResponse response) {
        response.addHeader("Content-Type", "text/html;charset=UTF-8");
        String folderName=request.getParameter("folderName");//文件夹名字
        String compress=request.getParameter("compress");//是否压缩图片
        // 上传图片信息
        Map<String, Object> uploadInfo = new HashMap<String, Object>();
        // 菜单栏目图标保存路径
        String path="";
        String iconPath =request.getSession().getServletContext().getRealPath("/Attachment");
        // 判断文件是否为空
        if (!Filedata.isEmpty()) {
            try {
                FileUploadUtil.uploadFileRandomFileName(Filedata, iconPath,folderName);
                // 返回保存文件信息
                path=FileUploadUtil.fileName;
                uploadInfo.put("fileInfo", File.separator+"Attachment"+File.separator+folderName+File.separator+path);
                
                if(compress!=null&&"true".equals(compress)){
                    //压缩图片
                    String filepath = iconPath+File.separator+folderName+File.separator+path;
                    FileUploadUtil.zipWidthHeightImageFile(filepath,filepath,30,40,10f);
                }
                
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return uploadInfo;
    }
    

猜你喜欢

转载自blog.csdn.net/qq_42556903/article/details/86602748