IE8上传word文件

背景:刚开始开发word上传功能时,用的formdata的格式传给后台

问题:后来客户那边说明情况,用的IE8,在用formdata传值时,在后端request不能强转为MultipartHttpServletRequest

调查原因:因为IE8不支持FormData,IE10才支持,因此此方法不兼容IE10以下的浏览器

解决方法:使用jquery.form.js支持的ajaxsubmit进行上传

html代码:

<form id="form1" name="form1" runat="server" method="post"  style="position:relative;" enctype="multipart/form-data">
                        <input type="file" name="fileName" id="fileName"  style="cursor:pointer;opacity:0;position: absolute;right: 144px;width:  90px;height: 70px;right:148px\0;filter:Alpha(opacity=0);" />
                         <button type="button" class="btn btn-default"  id="uploadBtn" style="display:none;"><a href="javascript:;"></a>上传</button>
                          </form>

js代码:必须导入jquery.form.js

$("#uploadDoc").click(function(){
             $("#fileName").click();
        });
        $("#fileName").change(function(){
             $('#uploadBtn').click();
        });
        $('#uploadBtn').click(function(){
             if($("#fileName").val()==""){
                $.messager.alert("操作提示","请点击浏览按钮 选择文件");
                return false;
            }
           
            var form = $("form[name=form1]");  
            var options  = {    
                url:'/flyywsjc/exceljc.do?method=inputExcelContent',    
                type:'post',
                dataType:"json",
                success:function(data){    
                    $(".kk_empty_mask .inner").click();
                           $("#replaceWords").click();
                           $("#newEditor").val(data.content);
                           if($("#newEditor").val()!=""){
                                 $("#matchNav").click();
                           }
                }    
            };    
            form.ajaxSubmit(options);
        });

java代码:

@RequestMapping(params = "method=inputExcelContent")
       public void inputExcelContent(@RequestParam("fileName") MultipartFile  multipartFile
                    ,HttpServletRequest request, HttpServletResponse response)  throws Exception{
             // 解决中文乱码问题
             request.setCharacterEncoding("utf-8");
             //response.setContentType("application/json;charset=utf-8");
             response.setContentType("text/plain;charset=utf-8");
             PrintWriter out = response.getWriter();
             Map<String, String> map = new HashMap<String, String>();
             try {
                     InputStream is = multipartFile.getInputStream();
                     String filename = multipartFile.getOriginalFilename();
                     if(filename.indexOf("docx")>-1){
                         XWPFDocument xdoc = new XWPFDocument(is);
                         List<XWPFParagraph> paras = xdoc.getParagraphs();  //This list will hold the paragraphs
                         XWPFWordExtractor ex = new XWPFWordExtractor(xdoc);   //To get the words
                         String content = ""; //This will hold all the text
                             for(XWPFParagraph p : paras){  //For each  paragraph we retrieved from the document
                                 content += p.getText()+" \n";        //Add  the text we retrieve to the words string  
                             }
                         System.out.println(content);
                         /*XWPFWordExtractor extractor = new  XWPFWordExtractor(xdoc);
                          String content = extractor.getText();*/
                          this.closeStream(is);
                          map.put("content", content);
                     }else if(filename.indexOf("doc")>-1){
                          WordExtractor extractor = new WordExtractor(is);
                          String content = extractor.getText();
                          this.closeStream(is);
                          map.put("content", content);
                     }
             } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (Exception e) {
             logger.info("query exceljc error message  = :" +  e.getMessage());
             e.printStackTrace();
        }
             // 将集合map转换为JSON格式的数据响应到客户端
             String cks = JSONObject.fromObject(map).toString();
             out.print(cks);
             out.close();
    }

注意:IE10可以支持application/json格式的Response,也就是说低于IE10版本的IE浏览器都需要使用text/html格式的Response。在Response头中指定Content-type为text/html,是可以解决问题的。这样返回给客户端的是一个JSON字符串(不是JSON对象),无需IE来解析。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值