简体   繁体   中英

Error in Sending Form file with form using AJAX

I am trying to submit form using AJAX that contains CSV File. So the idea is sending the form using ajax, process it in different file by generating a table and call back the processed table back into the page.

What i Have is this,

<form id="uploadXls" action="" method="post" enctype="multipart/form-data">
      <input id="uploaderFile" type="file" class="file"><br/>
      <button type="button" class="btn btn-orange pull-right" name="btnSubmit" id="btnSubmit"><i class="fa fa-download"></i> SHOW FILE CONTENT</button>
</form>

and the JavaScript is,

$("#btnSubmit").click(function(){
            $.ajax({
                type: 'POST',
                url: '../../content/maindiv_content/drawing/divpages/process_xls_file.php',
                data: new FormData(this),
                contentType: false,
                cache: false,
                processData: false,
                success: function (response, textStatus, jqXHR) {
                  $("#showFileContentTable").html(data);
                }
            });
        }); 

and im getting this kind of error in firebug,

TypeError: Argument 1 of FormData.constructor does not implement interface HTMLFormElement.
http://infserver/WeltesTankage/dist/js/jquery-1.10.2.min.js line 4 > eval
Line 14

What am i doing wrong here ? Please help me

Don't pass the files into the constructor, but use append, like:

var formData = new FormData();
formData.append('file', $('input[type=file]')[0].files[0]);
data:  formData

You can use this approach too.

var form = $('form').get(0); 

this code returns a jQuery object( $('form') ) and pass a HTML element to FormData ( get(0) ).

then in ajax request: data: new FormData(form),

You pass this to the FormData constructor. In this context, this is the button clicked, identified in the error message as a HTMLFormElement .

According to the documentation the FormData constructor expects a form element. So you have to change your code accordingly:

 var form = $("#uploadXls");

 $ajax({
     ...
     data: new FormData(form),
     ....
 });

may be this page help you..:)

<!DOCTYPE html>
<html>
<head>
  <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous">
  </script>
</head>
<body>
  <form method="post" id="fileinfo" enctype="multipart/form-data">
    file <input type="file" name="slug"><br>
     <input type="button" id="uploadBTN" value="Stash the file!"></input>
  </form>
<script type="text/javascript">
    $(document).ready(function()
  {
      $('#uploadBTN').on('click', function()
      { 
        var form = $('form').get(0); 
        console.log(form);
        var fd = new FormData(form);
        fd.append('user_id',4);
        fd.append('user_media_category_id',1);
        //console.log(fd);
        fd.append("user_", "This is some extra data");
        $.ajax({
            url: 'http://localhost/yii2/voila/project/api/web/v1/user-media/new',  
            type: 'POST',
            data: fd,
            success:function(data){
                console.log(data);
            },
            error:function(data){
                console.log(data);
            },
            cache: false,
            contentType: false,
            processData: false
        });
    });
    });
</script>
</body>
</html>

Try this form, it works for me without problems, Greetings, I hope I can help you.

//normalize form

var formulario = new FormData($('#form_img').get(0));    
    formulario.append('file', $('#customFile')[0].files[0]);

//AND add in Ajax call

data:formulario

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM