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?

Vadzim
  • 296
  • 1
  • 3
  • 11
  • possible duplicate of [Sending multipart/formdata with jQuery.ajax](http://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax) – PHP Worm... Jun 05 '15 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)`. – jeroen Jun 05 '15 at 13:02
  • Just in case. I tried a lot of variants of adding data but FormData is empty. – Vadzim Jun 05 '15 at 13:06
  • Check the network tab and see if the data is being sent to the server. – Musa Jun 05 '15 at 13:34
  • 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--` – Vadzim Jun 05 '15 at 13:45
  • Do you have any server side redirects? – Musa Jun 05 '15 at 14:02
  • 1
    Did you find the solution? – userlond Nov 21 '19 at 07:07

3 Answers3

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);
});
PHP Worm...
  • 4,109
  • 1
  • 25
  • 48
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);
});
Ashwani Goyal
  • 616
  • 4
  • 18
  • It didn't worked :( Also, the FormData obj looks like this: https://www.dropbox.com/s/g8gnrby1egwtveo/%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA.JPG?dl=0 – Vadzim Jun 05 '15 at 12:53
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);
});
Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200