I’m trying to upload a file with ajax but I’m not getting: "Undefined index: file in"

Asked

Viewed 36 times

-1

Form:

<form id="form" enctype="multipart/form-data">
        <input type="file" name="arquivo" multiple/>
        <br>
        <br>
        <input type="submit" id="linkTest" value="Consultar">
</form>

Ajax:

<script type="text/javascript">
$("#linkTest").on('click', function(e){     
    e.preventDefault();
        var form = $('form')[0];
        var formData = new FormData(form);

        $.ajax({
            type: 'post',
            url: 'upload.php',
            data: 'formData',
            proccessData: false,
            contentType: false,
            success: function (data) {
                alert(data);
            }
        });
    });
</script>

PHP:

<?php
$arquivo = $_FILES['arquivo'];
?>
  • Tip: Switch the event from the button to the form’s Submit event. This TB will make it easy to grab the data and submit.

  • 1

    You are sending a string and not the formData object. Remove the date quotes: 'formData'

  • TB there is that example, which is exactly what you want to do.

  • I removed the quotation marks and agr n receive no more answer. I will look at this article q showed me

  • The problem was in "proccessData: false," the right one would be: "processData: false,"

  • And it even works with form data as string?

  • In this case, add a reply and mark it as accepted. Highlight the problem in the typo.

  • And the question should be closed, because it is only a typo.

  • No! only after correcting the quotation marks and then correct the "proccessData: false," to "processData: false,".

Show 4 more comments

1 answer

0

Typo:

 data: 'formData',

for:

data: formData,

and in:

proccessData: false,

for:

processData: false,

Browser other questions tagged

You are not signed in. Login or sign up in order to post.