SpringMVC在用ajax上传文件的时候提示The current request is not a multipart request错误

一开始用传统ajax 上传,但是后台死活不认为这是上传文件,后来用了jquery 的 form 提交,成功,废话不说,直接上图

jsp:

                                        <form id="excelUpload" action="/admin/batchimport.do"  method="post" enctype="multipart/form-data">
                                                <input id="excel_file" type="file" name="filename" accept="xlsx" size="80"/>

                                        </form>
                                        <input id="excel_button" type="submit" value="导入Excel" onclick="check()"/>

ajax:

<script src="/static/plugins/form/jquery.form.min.js"></script>
<script type="text/javascript" src="/static/plugins/jQuery-confirm/xcConfirm.js"></script>

<script type="text/javascript">
    function check() {
        var txt = "确定提交吗?";
        var option = {
            onOk: function () {
                var excel_file = $("#excel_file").val();
                if (excel_file == "" || excel_file.length == 0) {
                    window.wxc.xcConfirm("请选择文件路径!", window.wxc.xcConfirm.typeEnum.error);
                    return false;
                } else {
                    var params = $("#excelUpload").serializeArray();
                    $("#excelUpload").ajaxSubmit({
                        method: 'post',
                        data:params,
                        url: '/admin/batchimport.do',
                        async: false,
                        success: function (data) {
                            if (data.code == 1) {
                                var txt = "批量导入成功!";
                                var option = {
                                    onOk: function () {
                                        window.location.href = "/admin/Student.jsp";
                                    }
                                };
                                window.wxc.xcConfirm(txt, "success", option);
                            } else {
                                if (data.code == 1) {
                                    var txt = data.msg;
                                    var option = {
                                        onOk: function () {
                                            window.location.href = "/admin/ImportStu.jsp";
                                        }
                                    };
                                    window.wxc.xcConfirm(txt, "error", option);
                                }
                            }
                        }
                    });
                    return true;
                }
            }
        };
        window.wxc.xcConfirm(txt, "warning", option);
    }
</script>

关键在:

$("#excelUpload").ajaxSubmit({
                        method: 'post',
                        data:params,
                        url: '/admin/batchimport.do',
                        async: false,
                        success: function (data) {
                            if (data.code == 1) {
                                var txt = "批量导入成功!";
                                var option = {
                                    onOk: function () {
                                        window.location.href = "/admin/Student.jsp";
                                    }
                                };
                                window.wxc.xcConfirm(txt, "success", option);
                            } else {
                                if (data.code == 1) {
                                    var txt = data.msg;
                                    var option = {
                                        onOk: function () {
                                            window.location.href = "/admin/ImportStu.jsp";
                                        }
                                    };
                                    window.wxc.xcConfirm(txt, "error", option);
                                }
                            }
                        }
                    });

 

posted @ 2017-06-05 17:45  写代码隔扣邓肯  阅读(10674)  评论(0编辑  收藏  举报