1

I am trying to upload file through jquery formdata without any form. My problem is that it is not sending any data to php file.

Here is my jquery code

jQuery(document).ready(function() {
    jQuery('#e_picture').change(function() {

        var file_data = jQuery('#e_picture').prop('files')[0];
        var form_data = new FormData();
        form_data.append('e_picture', file_data);
        form_data.append('e_uid', '3585');

        //});
        // data: {e_uid: e_uid, e_picture:'23'},

        jQuery.ajax({
            url: "index.php?option=com_objectified&task=course_reg.addPicture",
            type: 'POST',
            data: {
                form_data
            },
            processData: false,
            contentType: false,
            success: function(result) {
                alert('This is ' + result); // Here I show onlu e_uid but it alerts blank result
            }
        });
    });
});

Html

<input type="file" class="form-control" name='e_picture' id='e_picture'>
Zain Farooq
  • 2,956
  • 3
  • 20
  • 42

3 Answers3

2

You are sending the form_data in wrong way

instead of

data: {
   form_data
},

just send it like

data: form_data,
Krishan Kumar Mourya
  • 2,207
  • 3
  • 26
  • 31
1
jQuery(document).ready(function() {
jQuery('#e_picture').change(function() {

    var file_data = jQuery('#e_picture')[0].files;
    var form_data = new FormData();
    form_data.append("e_picture[]", file_data[0]);
    form_data.append('e_uid', '3585');

    //});
    // data: {e_uid: e_uid, e_picture:'23'},

    jQuery.ajax({
        url: "index.php?option=com_objectified&task=course_reg.addPicture",
        type: 'POST',
        data: {
            form_data
        },
        processData: false,
        contentType: false,
        success: function(result) {
            alert('This is ' + result); // Here I show onlu e_uid but it alerts blank result
        }
    });
});

});

Vijay Makwana
  • 911
  • 10
  • 24
0

ange content type to : contentType: 'multipart/form-data',