Formdata() object for sending files via ajax Jquery

Asked

Viewed 21,098 times

4

I need to send data from a form via ajax to the server (php). I used the Formdata() object but to no avail. Part of my jquery:

var formdata = new FormData($("#myform"));
var link = "form/insert";
        $.ajax({
            type: 'POST',
            url: link,
            data: formdata ,
            processData: false,
            contentType: false

        }).done(function (data) {
            $("div.container-fluid").html(data);
        });

my html has:

<input type="text" class="form-control" id="name" placeholder="name" name="name">
 <input type="file" id="arquivo" name="arquivo" />

Running the script only returns null. It would be possible to send the input text and the file via ajax?

  • 1

    Your link is with the correct url?

  • Yes! In my application I rewrite the link. I receive the data perfectly in php (the problem is the file upload)

  • 1

    @I’m dev c# but I’ll try to help you hehehe', in c# I do it like this. Look at this javascript

  • @Brunno your code is ok. I tested it here and everything right. Since I still can’t upload the form along with the file. I’m playing the file upload to Callback. But, I didn’t want to do it this way. I would have some hint?

  • @Brunonascimento try to do the following form If it works out, let me know and I’ll respond :)

1 answer

15


When retrieving the form it may return an array then take key 0 from it (if there is only one).

Note: Check that the form enctype is set to "Multipart/form-data"

//Exemplo do JS
var formdata = new FormData($("form[name='nome_do_form']")[0]);
var link = "form/insert";
    $.ajax({
        type: 'POST',
        url: link,
        data: formdata ,
        processData: false,
        contentType: false

    }).done(function (data) {
        $("div.container-fluid").html(data);
    });

Example of HTML:

<form name="nome_do_form" enctype="multipart/form-data">
     <input type="text" class="form-control" id="name" placeholder="name" name="name">
     <input type="file" id="arquivo" name="arquivo" />
     <input type="submit" />
</form>

Example of PHP:

<?php

echo "Texto: " . $_POST['name'] . '<br/><br/>';

if (move_uploaded_file($_FILES['arquivo']['tmp_name'], __DIR__."/upload_".date('YmdHis')."_".$_FILES['arquivo']['name'])) {
        echo "Arquivo recebido {$_FILES['arquivo']['name']} - o seu tamanho é de  {$_FILES['arquivo']['size']}";
} else {
        echo "Falha ao fazer upload!";
}
  • @Hiagosousa and after the user select how this selected data can be sent to mysql. My case is as http://answall.com/questions/181981/form-din%C3%A2mico-e-send-to-mysql-via-jquery-e-ajax? noredirect=1#comment376494_181981

  • Excellent, thanks

  • I am unable to send FILE via AJAX, I used according to this example and it did not work.

  • @Developerwebsistem... which S.O. are you using? Checked the write permissions in the folder where you want to upload?

  • @Hiagosouza hello friend, already solved the problem thanks for the feedback.

Browser other questions tagged

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