spring mvc 下使用ajaxfileupload.js 异步上传文件 并返回信息 各种问题解决

业务需要用ajax异步上传excel文件并回传解析后的错误信息,后台spring mvc, jquery 2.1.3, ajaxfileupload.js

一 页面js代码:

$("#importUserButton").click(function(){
    alert("here1");
    $.ajaxFileUpload({
            url:serverUri+"/user/importuser",
            type: "POST",
            cache : false,
            secureuri:false,
            fileElementId:"importUserFile",
            dataType: "json",
            success : function(data) {
                alert(data.success);
            },
            error : function(data) {
                alert("error");
            }
    });
});
二 后台代码:

IE10下返回json串会提示下载,所以ContentType设置为‘text’

@ResponseBody
@RequestMapping(value = "/importuser", method = RequestMethod.POST)
public Map<String,Object> importUser(HttpServletRequest request, HttpServletResponse response)
{
    response.setContentType("text/html;charset=UTF-8");
    MultipartHttpServletRequest mulRequest = (MultipartHttpServletRequest) request;
    MultipartFile file = mulRequest.getFile("importUserFile");
    String fileName = file.getOriginalFilename();
    System.out.println("fileName:"+fileName);
    Workbook wb = null;
    try {
        InputStream in = file.getInputStream();
        if (fileName.endsWith(".xls") || fileName.endsWith(".XLS")) {
            wb = new HSSFWorkbook(in);
        } else if (fileName.endsWith(".xlsx") || fileName.endsWith(".XLSX")) {
            wb = new XSSFWorkbook(in);
        }
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    ..........
    ..........

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("success", "true");
    return map;
}
三 ajaxfileupload.js 遇到过以下问题:

1.handleError 无法找到

2.上传后只执行error 函数

3.不支持IE(IE 下上传无反应)

附上改后全能的js文件。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值