如何在ajax中发送multipart请求?

时间:2014-10-11 16:04:10

标签: java javascript ajax servlets

给定的代码我需要上传zip文件,所以我需要从ajax向服务器发送多部分请求。但服务器获取请求null值如何解决此问题?是否有任何想法发送请求到服务器?

function importNow(serverURL, parameters) {
        document.body.style.cursor = "wait";
        $.ajax({
            url: serverURL,
            data: parameters,
            processData: false,
            contentType: false,
            type: "POST",
            cache: false,
            dataType: "text",
            success: function(data) {
                if ($.trim(data) === "Success") {
                    updateStatusMessage("success", "Import scenario successfully");
                } else {
                    updateStatusMessage("failure", $.trim(data));
                }
                document.body.style.cursor = "default";
            },
            async: false
        });
    }

    function importScenario() {
        //window.location.href = clientURL + "/common/jsp/import.jsp";
        var serverURL = "http://localhost:8080/bwsim/UploadScenario";
        var parameters = "requestType=Import&subRequestType=importScenario&userName=" + userName ;
         refButton = '<form id="importForm" class="userInputForm" enctype="multipart/form-data">' +
         '<input id="file" name="file" type="file" />' +
         '</form>';
         document.getElementById("popupDiv").innerHTML = refButton;
         $("#popupDiv").dialog({
             title: "Import Scenario",
             draggable: true,
             bgiframe: true,
             modal: true,
             width: 500,
             heigth: 100,
             show: {effect: 'blind', duration: 500},
             hide: {effect: 'fade', duration: 1000},
             zIndex: 1000,
             buttons: {
                 'Upload': function() {
                     if ($("#importForm").valid()) {
                         parameters; 
                         importNow(serverURL, parameters);
                         $(this).dialog("close");
                     }
                 },
                 'Cancel': function() {
                     $(this).dialog("close");
                 }
             }
         });
    }

2 个答案:

答案 0 :(得分:0)

您可以轻松实现jquery ajax form plugin。使用此插件,您可以轻松地通过ajax调用和表单发送额外的$_POST数据。

答案 1 :(得分:0)

要执行multipart / formdata ajax请求(带文件),您必须使用FormData对象。您可以手动将字段添加到对象,然后通过ajax发送它,或者您可以使用表单元素构建它。

     ...
     var serverURL = "http://localhost:8080/bwsim/UploadScenario";
     var refButton = '<form id="importForm" class="userInputForm" enctype="multipart/form-data">' +
     '<input id="file" name="file" type="file" />' +
     '<input value="Import" name="requestType" type="hidden" />' + 
     '<input value="importScenario" name="subRequestType" type="hidden" />' + 
     '<input value="'+userName +'" name="userName" type="hidden" />' + 
     '</form>';
     document.getElementById("popupDiv").innerHTML = refButton;
     $("#popupDiv").dialog({
         title: "Import Scenario",
         draggable: true,
         bgiframe: true,
         modal: true,
         width: 500,
         heigth: 100,
         show: {effect: 'blind', duration: 500},
         hide: {effect: 'fade', duration: 1000},
         zIndex: 1000,
         buttons: {
             'Upload': function() {
                 if ($("#importForm").valid()) {
                     var parameters = new FormData($('#importForm')[0]); // formdata object constructed with form
                     importNow(serverURL, parameters);
                     $(this).dialog("close");
                 }
             },
             ...