使用AJAX上传多个文件

时间:2015-07-23 09:51:58

标签: javascript jquery ajax jsp multiple-file-upload

我正在使用网络应用程序并且我使用多个文件上传,但它不能与AJAX一起使用。 对于多个文件上传,我使用Apache FileUpload,它运行得很好,但在使用Ajax后,ServletFileUpload.isMultipartContent()返回False。

感谢您的帮助

这是我的JSP代码:

<!DOCTYPE html>
<html>
    <head>
    <meta charset="utf-8" />
    <title>Envoi des fichiers RNP</title>
        <script src="js/jquery-1.11.3.min.js"></script>
        <script src="js/fileupload.js"></script>

        <link type="text/css" rel="stylesheet" href="css/form.css">
    </head>
    <body>
        <form id="myForm" >
            <fieldset>
                <legend>Envoi de fichier</legend>
                <label for="fichier">Emplacement du premier fichier <span class="requis">*</span></label>
                <input type="file" id="fichier" name="fichiers[]" multiple value="<c:out value="${fichier.nom}"/>"/>
                <span class="erreur">${form.erreurs['fichier']}</span>
                <br />
                <br />
                <input type="submit" value="Envoyer" id="showTable"/>
                <br />
            </fieldset>
        </form>
        <div id="tablediv">
            <table cellspacing="0" id="site2G">
                <tr>
                    <th scope="col">CGI</th>
                    <th scope="col">BSC</th>
                    <th scope="col">Site Name</th>
                    <th scope="col">Cells</th>
                    <th scope="col">EA</th>         
                </tr>
            </table>
        </div>      
    </body>
</html>

和我的AJAX代码:

 $(document).ready(function() {$("#tablediv").hide();
 $("#myForm").submit(function(event){
     event.preventDefault();

     var formData = new FormData($(this)[0]);

     $.ajax({
            url: "Upload",
            type: 'POST',
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function(responseJson) {
                if(responseJson!=null){
                    $("#site2G").find("tr:gt(0)").remove();
                    var table1 = $("#site2G");
                    $.each(responseJson, function(key,value) {
                         var rowNew = $("<tr><td></td><td></td><td></td><td></td><td></td></tr>");
                            rowNew.children().eq(0).text(value['bsc']);
                            rowNew.children().eq(1).text(value['sitename']);
                            rowNew.children().eq(2).text(value['cells']);
                            rowNew.children().eq(3).text(value['cgi']);
                            rowNew.children().eq(4).text(value['ea']);
                            rowNew.appendTo(table1);
                    });
                    }
                }
          });
        $("#tablediv").show();}); });

3 个答案:

答案 0 :(得分:1)

您可以更喜欢第三方插件将多个文件上传到服务器

我推荐的一些是

他们支持

  

多个文件上传。

     

进度条

     

文件成功上传后该怎么办

     

图片预览

答案 1 :(得分:0)

我使用以下功能上传

$.upload = function(url, form, _callback) {
    $.ajax({
        url: url, 
        type: "POST", 
        processData:false,
        cache: false,
        async: true,
        data: form,
        contentType: false,
        success:function(data, textStatus, request){
            if(typeof _callback == "function")
                _callback.call(this, data);
        },
        error:function(data, textStatus, request){
            if(typeof _callback == "function")
                _callback.call(this, data);
        }
    });
};

调用此功能

var formData = new FormData();
for(var i=0;i<$("#upload #file").get(0).files.length;i++)
   formData.append("file" + i, $("#upload #file").get(0).files[i]);


$.upload("Upload", formData, function(data){
    //success or failure check
});

您的HTML

<form id="upload" style="display:none">
    <input name="file" id="file" type="file" multiple />
</form>

答案 2 :(得分:0)

像狄更斯提出的那样,但代码格式略有不同:

    var formData = new FormData();
    var $uploader = $("#FileUpload_doc")[0];
    for (let i = 0; i < $uploader.files.length; i++)
        formData.append("file_" + i, $uploader.files[i]);

    $.ajax({
        url: 'your url here',
        processData: false,
        contentType: false,
        data: formData,
        type: 'POST'
    }).done(function (dataReceived) {
     // do another things here
    }).fail(function (a, b, c) {
        alert(a, b, c);
    });
相关问题