Issue uploading file using JQuery ajax.

I getting crazy trying different way to make a simple upload of a file using jquery / ajax,

The form:
 <form id="formupload" method="post" enctype="multipart/form-data">
            <input type="file" name="csvbrowser" id="csvbrowser" style="width: 95px;"/>
            <input type="submit" value="Submit">
        </form>
   

Open in new window


THe Jquery code

jQuery('#formupload').submit(function (event) {
            event.preventDefault();
            var filename = new FormData(document.getElementById("csvbrowser"));

            jQuery.ajax({
                url: "loadproduitscsv.php",
                type: "POST",
                data: {
                    file: filename
                },
                enctype: 'multipart/form-data',
                processData: false, // tell jQuery not to process the data
                contentType: false   // tell jQuery not to set contentType
            }).done(function (data) {
                alert(data);
            });

        });

Open in new window

bruno_boccaraAsked:
Who is Participating?
 
Vijaya KumarCommented:
run this one...
jQuery('#formupload').submit(function (event) {
            var form = $('#formupload')[0];
            var filename = new FormData(form);
            jQuery.ajax({
                url: "loadproduitscsv.php",
                type: "POST",
                data: filename,
                enctype: 'multipart/form-data',
                processData: false, // tell jQuery not to process the data
                contentType: false   // tell jQuery not to set contentType
            }).done(function (data) {
                alert(data);
            });
         event.preventDefault();
        });

Open in new window

1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.