4

When trying to upload an image using AJAX without submitting the form directly and sending a FormData object to server it returns empty $_FILES array. But if I submit the form using <input type="submit"> tag $_FILES array is not empty and recieves the data.

HTML

<form action="core/update.php" method="post" enctype="multipart/form-data" id="profile-photo" name="profile-photo-form">
  <input type="file" id="photo-filename" name="avatar" class="edit-show panel photo-upload">
</form>
<button class="save-button" disabled="disabled">Save</button>

JS

$('#profile-photo').on('submit', function(e) {
    e.preventDefault();

    var form = $('#profile-photo')[0];
    var formData = new FormData(form);

    formData.append('avatar', $('#photo-filename')[0].files[0]);

    $.ajax({
      url: "core/update.php", 
      data: formData,
      type: "POST", 
      contentType: false,       
      cache: false,             
      processData: false
    });

    console.log(formData);
});

$('.save-button').on('click', function() {
    if ($('#photo-filename').val != '') {
        $('#profile-photo').submit();
    };
}

UPD

Also $('#profile-photo').serialize() returns blank string.

UPD 2

Can it conflict with the other AJAX-requests on the page?

7
  • possible duplicate of Sending multipart/formdata with jQuery.ajax Commented Jun 5, 2015 at 13:02
  • 1
    Why are you adding the file input twice? It is already in the form that you add using new FormData(form). Commented Jun 5, 2015 at 13:02
  • Just in case. I tried a lot of variants of adding data but FormData is empty. Commented Jun 5, 2015 at 13:06
  • 1
    Musa, the request is being sent. Request payload: ------WebKitFormBoundarykKBm9n5RL1SEIQD5 Content-Disposition: form-data; name="avatar"; filename="s369unD7-50[2].jpg" Content-Type: image/jpeg ------WebKitFormBoundarykKBm9n5RL1SEIQD5-- Commented Jun 5, 2015 at 13:45
  • 1
    Did you find the solution? Commented Nov 21, 2019 at 7:07

3 Answers 3

2

Try this:

Because user may upload multiple files

jQuery.each(jQuery('#photo-filename')[0].files, function(i, file) {
    data.append('file-'+i, file);
});

Instead of

formData.append('avatar', $('#photo-filename')[0].files[0]);

Complete Solution:

$('#profile-photo').on('submit', function(e) {
    e.preventDefault();

var form = $('#profile-photo')[0];
var formData = new FormData(form);

 jQuery.each(jQuery('#photo-filename')[0].files, function(i, file) {
    formData.append('file-'+i, file);
});

$.ajax({
  url: "core/update.php", 
  data: formData,
  type: "POST", 
  contentType: false,       
  cache: false,             
  processData: false
});

    console.log(formData);
});
Sign up to request clarification or add additional context in comments.

8 Comments

I guess there is nothing wrong in your code you might be checking contents on update.php by $_POST you must use $_FIELS
No, I'm checking $_FILES array and it returns Array()
Is this written on your update.php if($_SERVER['REQUEST_METHOD'] == 'POST'){ echo "<pre>"; print_r($_FILES); }
change it to print_r($_FILES);
This is what I have mentioned in my first comment
|
0

Try the following,

$('#profile-photo').on('submit', function(e) {
    e.preventDefault();
    var formData = new FormData(this); // here I am editing

    formData.append('avatar', $('#photo-filename')[0].files[0]);

    $.ajax({
      url: "core/update.php", 
      data: formData,
      type: "POST", 
      contentType: false,       
      cache: false,             
      processData: false
    });

    console.log(formData);
});

1 Comment

It didn't worked :( Also, the FormData obj looks like this: dropbox.com/s/g8gnrby1egwtveo/…
0

If it is only the file you want to send then you can do it as below and no need to attach form here to formdata:

$('#profile-photo').on('submit', function(e) {
    e.preventDefault();
    var formdata = new FormData();
    var fileInput = $('#photo-filename');


    //Use Either this
    $.each($(fileInput).get(0).files, function (index, value) {
           formdata.append('avatar', value);
    });

    //Or this
    $.each($(fileInput).get(0).files, function (index, value) {
        formdata.append(value.name, value);
    });

    //For single file use this
    formData.append('avatar', $('#photo-filename')[0].files[0]);

    $.ajax({
      url: "core/update.php", 
      data: formData,
      type: "POST", 
      contentType: false,       
      cache: false,             
      processData: false
    });

    console.log(formData);
});

4 Comments

Still nothing. Neither the first solution nor the second.
Can you just show how you are submiting the form??
Clicking on <button class="save-button"></button> object fires a function if ($('#photo-filename').val != '') { $('#profile-photo').submit(); };
Can you edit the question and insert it in question? the whole process??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.