zoukankan      html  css  js  c++  java
  • layui结合SpringMVC上传文件以及携带额外的参数上传文件

      今天在使用layui的过程中,遇到了使用其上传文件的模块。自己感觉文件上传还是bootstrapfileinput插件比较好用一些,灵活方便,bootstrapfileinput使用方法参考:http://www.cnblogs.com/qlqwjy/p/8410413.html

      在使用layui的文件上传模块的时候想要携带额外的参数,在这里鼓捣了半天。。。真的是坑。在这里还是总结一下layui文件上传模块的使用方法。

    1.页面准备文件上传的模态框:

    <!--相关的几个模态框-->
    <!--1 S    上传图片的模态框-->
    <%--隐藏打开的index--%>
    <input type="hidden" id="hidden_picture_index">
    <div class="x-body" style="display: none" id="pictureModal">
        <div class="layui-upload">
            <!--隐藏培养方案编号-->
            <input type="hidden" id="hidden_trainSchemeId">
            <!--隐藏上传文件的操作-->
            <input type="hidden" id="hidden_tmp_index">
            <button type="button" class="layui-btn" id="structurePicture"><!--动态赋值--></button>
            <!--预留一个预览的img标签-->
            <div id="imgDiv" style="display: none;margin-top: 10px;">
                <img id="imgPreview" width="400px" height="200px">
            </div>
            <div id="promptDiv" style="margin-top: 10px;display: none">
                <p><font style="font-size: 30px">您还没有上传课程关系结构图,请先上传课程关系结构图!</font></p>
            </div>
        </div>
    </div>
    <!--1 E    上传图片的模态框-->

    2.上传文件的JS

    layui.use(['upload','layer'],function () {
        var upload = layui.upload,layer = layui.layer;
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#structurePicture',//绑定的元素
            url: contextPath+'/TrainPicture/uploadTrainPicture.do',//提交的url
            auto:true,//是否自动上传
            accept:"images",//指定允许上传的文件类型
            multiple:false,//支持多文件上传
            acceptMime:"image/*",//规定打开文件选择框时,筛选出的文件类型
    /*        data:{
                trainningschemeid:  $("#hidden_trainSchemeId").val()
            },//额外携带的培养方案编号*/
            before: function(obj){
                this.data={"trainningschemeid": $("#hidden_trainSchemeId").val()}//携带额外的数据
                //预读本地文件示例,不支持ie8
                obj.preview(function(index, file, result){
                    $('#imgPreview').attr('src', result); //图片链接(base64)
                });
                var index = layer.load(); //开始上传之后打开load层
                $("#hidden_tmp_index").val(index);//将load层的index隐藏到页面中
            },
            done: function(res, index, upload){ //假设code=0代表上传成功
                layer.close(layer.index); //它获取的始终是最新弹出的某个层,值是由layer内部动态递增计算的
                if(res.msg=="上传成功"){
                    //提示上传成功(关闭之后将按钮的内容变为更换图片)
                    layer.msg(res.msg, {icon: 1,time:2*1000},function () {
                        $("#structurePicture").text("更换主要课程关系结构图");//按钮标题置为上传图片
                        $("#promptDiv").css("display","none");//隐藏提示语
                        $("#imgDiv").css("display","");//显示图片框
                    });
                }
    
            }
        });
    })
    
    
    
    
    /*************E  上传培养方案课程结构图相关操作***************/

      data属性用于携带额外的参数,可是我带的参数总是""(空串)。后来查阅网上资料发现第二种解决办法就是在before文件上传的函数中动态设置上传的参数。例如上面的before函数。但是有的时候上面的data属性可以生效,有的时候却不生效。。。。。。所以在携带参数的时候需要参考上面两种方法看哪种生效。

    注意:因为layui支持的格式默认为图片文件(JPG),在代码中添加accept:'file'。有时候不添加会警告 选择的图片中包含不支持的格式

     

    3.接收文件的后台:(Java代码)

        @RequestMapping("/uploadTrainPicture")
        public ResposeResult addTrainPicture(Trainpicture trainpicture, MultipartFile file){
            //1.保存图片到本地
            String fileoriname=null;//原名称
            String filenowname=null;//系统生成的名称
            if(file != null){
                fileoriname = file.getOriginalFilename();//获取原名字
                filenowname = UUIDUtil.getUUID2()+ "."+FilenameUtils.getExtension(fileoriname);//UUID生成新的唯一名字+文件扩展名
            }
    
            try {
                 FileHandleUtil.uploadSpringMVCFile(file, "trainPicture", filenowname);
            } catch (Exception e) {
                logger.error("保存培养方案图片出错",e);
            }
            //2.调用service保存数据库
            trainpicture.setPictureoriname(fileoriname);
            trainpicture.setPicturenowname(filenowname);
            trainpicture.setUptime(new Date());
            ResposeResult resposeResult = new ResposeResult();
            String result = null;
            try {
                result = trainPictureService.addTrainPicture(trainpicture)?"上传成功":"上传失败";
            } catch (SQLException e) {
                result = "上传失败";
                logger.error("保存培养方案数据库出错",e);
            }
            resposeResult.setMsg(result);
            return resposeResult;
        }

      

      发现在使用bootstrapfileinput的多文件上传插件和layui的多文件上传插件的原理都是每次传一个文件,多个文件会访问多次,所以java的MultipartFile也不必用数组接收。

  • 相关阅读:
    [ SHOI 2012 ] 随机树
    [ BZOJ 4318 & 3450 / CodeForces 235 B ] OSU!
    [ HNOI 2015 ] 亚瑟王
    [ JSOI 2015 ] Salesman
    [ ZJOI 2007 ] 时态同步
    [ Luogu Contest 10364 ] TG
    [ CodeForces 17 E ] Palisection
    [ BZOJ 2160 ] 拉拉队排练
    Manacher 学习笔记
    [ CodeForces 865 D ] Buy Low Sell High
  • 原文地址:https://www.cnblogs.com/qlqwjy/p/8992956.html
Copyright © 2011-2022 走看看