user1742983
user1742983

Reputation: 51

$_FILES and $_POST using ajax

in my form, I added input file.

<input name="uploadedFile" type="file" size="50" accept="application/pdf" />

And I have this ajax:

$("#submit").click(function() {
  $.ajax({
    type: "POST",
    url: "submit.php",
    data: $("form").serialize(),
    success: function(result){
       alert(result);
    });
    return false;
});

How can I pass the file data to submit.php and upload it using the code in submit.php. Thanks.

Upvotes: 0

Views: 1306

Answers (1)

user229044
user229044

Reputation: 239291

jQuery serialize doesn't support file type inputs. From the docs:

Data from file select elements is not serialized.

You will need to roll your own solution using the HTML5 FileReader API, to read the contents of the input as a base64-encoded string.

More likely, you will want to use traditional HTML form submission and avoid AJAX altogether for this.

Upvotes: 7

Related Questions