拖拽上传功能旨在实现拖拽文件或者图片上传到你想要保存到的地方。此处上传的是xml文件,可以在里面对文件类型进行限制;

声明:现在的ie浏览器9及以下的版本并不支持该方法实现拖拽上传,如果有大神可以分享ie9及以下的拖拽上传方法将不胜感激;

代码:

<body>
<div )
                 return false;
            }

    //此处是ie9及以下不支持的方法的地方;e.dataTransfer.files获取不到。
             fileList=e.dataTransfer.files; 
            for(var i=0;i<fileList.length;i++){ 
                //此处判断只能上传图片 
                if(fileList[i].name.indexOf(".xls")<0){ 
                    alert("请上传excle文件"); 
                    return;              
                } 
                //图片预览  这一步需要判断是什么浏览器  大家自己判断吧 
                /*************************************/
                //    img1=window.URL.createObjectURL(fileList[i]);
                    img1="../app/img/xlsIMG11.png";//我在此处给引入的 文件配置了一张文件类型的图标
                    img2="../app/img/deleteIMG11.png";
                function deleteFile(){
                      document.getElementById("preview").innerHTML ="";
                      fileList=null;
                }
                /*************************************/ 
                var str="<div><img src='"+img1+"'/><p>"+fileList[i].name+"</p><button onclick='deleteFile()'>删除</button></div>"; 
                var str1=fileList[i].name; 
                document.getElementById("preview").innerHTML +=str; 
               // document.getElementById("preview").value=str1;
                var fileName=fileList[i].name; 
               console.info(fileName); 
               var fileSize=fileList[i].size; 
               console.info(fileSize);
               document.getElementById("filename").value=fileName;
               //4、通过XMLHttpRequest上传文件到服务器  就是一个ajax请求 
            } 
        }); 
    } 
    //直接调用 
    tm_upload(); 
    function deleteFile(){
        document.getElementById("preview").innerHTML ="";
         document.getElementById("filename").value="";
    }
   
    function subSelect(){  debugger
        if( document.getElementById("filename").value==""){
            fileList.length=0
            alert("选择文件不能为空")
            return false;
        }
   
        var FileIsNull = document.getElementById("doc").value;
        var length =fileList.length;
        if(FileIsNull==null||FileIsNull==""){
        for(var j=0 ; j<length;j++){ 
            //此处判断只能上传图片 
            if(fileList[j].name.indexOf(".xls")<0){ 
                alert("请上传.xls文件"); 
                return;              
            } 
            //图片预览  这一步需要判断是什么浏览器  大家自己判断吧 
            /*************************************/
            //img1=window.URL.createObjectURL(fileList[i]);
     
           
            /*************************************/ 
            //var str="<div><img src='"+img1+"'/><p>"+fileList[j].name+"</p></div>"; 
            //var str1=fileList[j].name; 
           // document.getElementById("preview").innerHTML +=str; 
           // document.getElementById("preview").value=str1;
            var fileName=fileList[j].name; 
           console.info(fileName); 
           var fileSize=fileList[j].size; 
           console.info(fileSize);
           //4、通过XMLHttpRequest上传文件到服务器  就是一个ajax请求 
           var xhr=new XMLHttpRequest(); 
           //5、这里需要先写好一个importExcel.jsp的上传文件,当然,你写成servlet或者是action都可以 
           xhr.open("post","<%=path%>/examScore/scoreImport",true); 
           xhr.setRequestHeader("X-Request-Width", "XMLHttpRequest"); 
           //6、通过HTML5 FormData动态设置表单元素 
           var formData=new FormData();//动态给表单赋值,传递二进制文件 
           //其实就相当于<input type="file" name="file"/> 
           formData.append("upload",fileList[j]); 
           xhr.send(formData);
           return false;
        }
        }else{
            return true;
        }
    };
    mini.parse();


    
/*        var file=$(":file").val();
       if(""==file){
           alert("文件不能为空");
           return false;
       } */
/*        var pos = file.lastIndexOf('.xls');
       var pos1=file.lastIndexOf('.xlsx');
       if(pos==-1&&pos1==-1){
           alert("文件类型不匹配");
           return false;
       }
      
       if(pos + 4 != file.length&&pos1+5!=file.length){
           alert("文件类型不匹配");
           return false;
       } */
        //3、从事件event中获取拖拽到浏览器的文件信息 
   /*      var fileNum= document.getElementById("preview").childNodes.length
        if(fileNum>0){
            alert("只能上传一个文件")
             return false;
        } */

 
    function showAddPlan(){
        $("#table1").hide();
        $("#addPlan").show();
    }
    function getPlanList(){
        var planSort='<%=planSort%>';
        var url="<%=path %>/examPlan/showPlanList?planSort=<%=planSort%>";
        mini.open({
            url: url,
            title: "计划名称列表", width: 800, height: 450,
            ondestroy: function (action) {
            }
        });
        }
    function onCancel() {
        window.CloseOwnerWindow("ok");
    }
</script>
<script type="text/javascript">
    mini.parse();
    function beforeSubmit(){
       var file=$(":file").val();
       if(""==file){
           alert("文件不能为空");
           return false;
       }
       var pos = file.lastIndexOf('.xls');
       var pos1=file.lastIndexOf('.xlsx');
       if(pos==-1&&pos1==-1){
           alert("文件类型不匹配");
           return false;
       }
       if(pos + 4 != file.length&&pos1+5!=file.length){
           alert("文件类型不匹配");
           return false;
       }
   }
    function onCancel() {
        window.CloseOwnerWindow("ok");
    }
    function mySubmit(){
        $("#form1").submit();
        window.CloseOwnerWindow("ok");
    }
</script>
</body>

后台你们自己写吧,你们用什么框架我有不知道,只要接收一下这里传过去的xml文件就行了,我用的spring_mvc框架所以在后台接收是这样的

注意:标注部分是框架接收文件类型的必备注解,

public void scoreImport(@RequestParam("upload") MultipartFile mFile, Model model, HttpServletRequest request,
            HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        StaticEntity.setPagestatus(3);// 3代表jsp页面可关闭
        InputStream is = excelUtil.processFileUpload(mFile, request);
        Map map = new HashMap();
        map = readScoreExcelData(is, map);
        try {
            if (map.get("content") != "") {
                StaticEntity.setPagestatus(2);
            } else {
                List lists = (List) map.get("lists");
                String planId=map.get("planId").toString();
                String status=scoreService.getPlanStatus(planId);
                if(status==null||status==""){
                    StaticEntity.setPagestatus(4);//计划不存在
                }else if("1".equals(status)){
                    StaticEntity.setPagestatus(5);//计划已发布成绩
                }else{
                    request.setAttribute("planId", planId);
                    scoreService.insertScoreData(lists, request);// 县
                    StaticEntity.setPagestatus(1);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

分类:

技术点:

相关文章: