Jquery 使用ajax和Javaservlet上传文件

Jquery 使用ajax和Javaservlet上传文件,jquery,ajax,jsp,servlets,Jquery,Ajax,Jsp,Servlets,我试图使用扩展HttpServlet的servlet从jsp页面上传一个csv文件。在jsp页面中,我使用的是一个ajax,它应该调用servlet 这是ajax部分: $(function() { $(".upldBtn").click(function() { alert("Upload button pushed"); $.ajax({ type: "POST", url: contextP

我试图使用扩展HttpServlet的servlet从jsp页面上传一个csv文件。在jsp页面中,我使用的是一个ajax,它应该调用servlet

这是ajax部分:

    $(function() {
    $(".upldBtn").click(function() {

        alert("Upload button pushed");

        $.ajax({
            type: "POST",
            url: contextPath + servletPath,
            data: "action=get&custIdList=" + $('#custIdList').val(),
            async: false,
            dataType: "text/csv; charset=utf-8", 
            success: function(data){
                  alert("success");
              }
        });
    });
contextPath和servletPath也被声明,我没有在这里指定它们

在jsp页面中,我将此表单放在一个表中:

<form method="post" action="CSRUploadListServlet" enctype="multipart/form-data">
<input type="file" name="custIdList" id="custIdList" />
<input type="submit" value="Upload" class="upldBtn" />
</form>
如果(!ServletFileUpload.isMultipartContent(request))返回“请求不包含上载数据”,则所有这些都会被卡住

我确信我没有正确地编写ajax,但我似乎无法找出哪里做错了


谢谢。

嗨!尝试以不同的方式编写html代码,然后从ajax调用servlet,就像您在那里做的那样。我认为问题可能在于你使用的形式,即重写某些属性或类似的东西

我建议使用一个iframe选项,从js代码加载。html代码可以如下所示:

<button id="upldBtn" title="Upload" >Do the upload</button>

<div id="textarea" style="display: none;"></div>

<input type="file" class="file" id="file" name="file" title="Please upload"/>
进行上传
以及javascript代码:

    $(function() { 

    $('#upldBtn').click(function() {
        var contextPath = 'your path string';
        var servletName = 'your servlet name string';
        var iframe = $('<iframe name="postiframe" id="postiframe" style="display: none" />');
        $("body").append(iframe);

         $("form#yourform").attr('action', contextPath+servletName);
         $("form#yourform").attr('enctype', "multipart/form-data");
         $("form#yourform").attr("target", "postiframe");
         $("form#yourform").attr("file", $('#file').val());

        $('yourform').submit(); //upload button 
             $("#postiframe").load(function () {
                    iframeContents = $("#postiframe")[0].contentWindow.document.body.innerHTML;
                    $("#textarea").html(iframeContents);
                            $.ajax({
                                    type: "GET",
                                    url: contextPath+servletName,
                                    data: "action=download",
                                    async: false,
                                    dataType: "text",
                                    success: function(result) {
                                        //do something
                                    }
                                });
                } });
            }); 
});
$(函数(){
$('#upldBtn')。单击(函数(){
var contextPath='您的路径字符串';
var servletName='您的servlet名称字符串';
变量iframe=$('');
$(“正文”)。附加(iframe);
$(“form#yourform”).attr('action',contextPath+servletName);
$(“表单#yourform”).attr('enctype',“多部分/表单数据”);
$(“form#yourform”).attr(“target”、“postiframe”);
$(“form#yourform”).attr(“file”,$(“#file”).val();
$('yourform').submit();//上传按钮
$(“#positframe”).load(函数(){
iframeContents=$(“#PostFrame”)[0].contentWindow.document.body.innerHTML;
$(“#textarea”).html(iframeContents);
$.ajax({
键入:“获取”,
url:contextPath+servletName,
数据:“动作=下载”,
async:false,
数据类型:“文本”,
成功:功能(结果){
//做点什么
}
});
} });
}); 
});
告诉我这对你是否合适。:)
欢呼声

当您按下提交按钮时,会调用
CSRUploadListServlet
contextPath+servletPath
,我可以知道为什么吗?是的,为了在我按下上传提交按钮时使用servlet。您不能像这样通过Ajax上传文件。您将该文件视为普通参数。那不行。好吧,我现在可以看到了,谢谢。但是如果我避免使用Ajax,那么表单就不会通过servlet访问
    $(function() { 

    $('#upldBtn').click(function() {
        var contextPath = 'your path string';
        var servletName = 'your servlet name string';
        var iframe = $('<iframe name="postiframe" id="postiframe" style="display: none" />');
        $("body").append(iframe);

         $("form#yourform").attr('action', contextPath+servletName);
         $("form#yourform").attr('enctype', "multipart/form-data");
         $("form#yourform").attr("target", "postiframe");
         $("form#yourform").attr("file", $('#file').val());

        $('yourform').submit(); //upload button 
             $("#postiframe").load(function () {
                    iframeContents = $("#postiframe")[0].contentWindow.document.body.innerHTML;
                    $("#textarea").html(iframeContents);
                            $.ajax({
                                    type: "GET",
                                    url: contextPath+servletName,
                                    data: "action=download",
                                    async: false,
                                    dataType: "text",
                                    success: function(result) {
                                        //do something
                                    }
                                });
                } });
            }); 
});