Hideki Kan
Hideki Kan

Reputation: 43

Ajax don't send file

I am trying to do a solution in Ajax to send a file when the user send a file and upload automatically when change the status

Code

<script>
function filePassSelected() {
    var data = $('frmpass')[0];
    var formData = new FormData(data);
    alert (formData);
        $.ajax({
        url: "upload.php",
        type: "POST",
        data: formData,
        success:function(data){
            $('#verdoc').html(data);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert(textStatus, errorThrown);
        }
    });
};
</script>
<form action="upload.php" method="post" enctype="multipart/form-data" id="frmpass">
  <label for="fileToUploadPass" class="btn" id="labelfilesid">
    <div class="buttonfile" style="margin:auto">National ID</div>
  </label>
  <input id="fileToUploadPass" style="visibility:hidden;" type="file" name="fileToUploadPass" onchange="filePassSelected()">
</form>
<div id="verdoc"></div>

Otherwise, the form is not sending data, I change data in the Ajax to send random information and works, but if send the file don't work. What I am doing wrong?

Upvotes: 0

Views: 134

Answers (1)

Cagri Tacyildiz
Cagri Tacyildiz

Reputation: 17570

add contentType and processData as false

 $.ajax({
        url: "upload.php",
        type: "POST",
        processData: false,
        contentType: false,
        data: formData,
        success:function(data){
            $('#verdoc').html(data);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert(textStatus, errorThrown);
        }});

Upvotes: 1

Related Questions