0

I'm working on a webapp and I'm using multiple files upload, but it doesn't work with AJAX. For multiple files upload i use the Apache FileUpload which is working perfectly but after using Ajax the ServletFileUpload.isMultipartContent() returns False.

Thanks for your help

Here is my JSP code :

<!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>

and my AJAX code :

 $(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();}); });
1
  • You need to check in the network tab those files are getting uploaded there or not. Commented Jul 23, 2015 at 10:08

3 Answers 3

1

You can prefer third party plugins to upload your multiple files to server

A few that i will recommend are

They support

multiple file upload.

progress bar

what to do after file has successfully uploaded

Image preview

Sign up to request clarification or add additional context in comments.

6 Comments

For security reason you cannot use jquery ajax how come? Where did you find it? Don't you know its possible with FormData()+jQuery.ajax()?
@Jai No i didnt know that and i googled it yes it is possible.. and i have edited my answer ... sorry for wrong information
and only with html5.
Thanks @Jai so what you recommend, using a plugin or my own custom jquery code for file upload next time
@Jai Thank you for your answer. I choosed to work with a JQuery plugin and it worked perfectly. thank you so much !
|
0

I used the following function for upload

$.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);
        }
    });
};

To call this function

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
});

Your HTML is

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

2 Comments

issue is different. there is multiple attribute in the file.
it is also possible by looping through files[i] and naming the append with unique field names like file1, file2, file3
0

like Dickens proposed , but the code is little differently formatted:

    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);
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.