jquery ajax send file code example

Example 1: ajax file upload jquery

$('#upload').on('click', function() {
    var file_data = $('#sortpicture').prop('files')[0];   
    var form_data = new FormData();                  
    form_data.append('file', file_data);
    alert(form_data);                             
    $.ajax({
        url: 'upload.php', // point to server-side PHP script 
        dataType: 'text',  // what to expect back from the PHP script, if anything
        cache: false,
        contentType: false,
        processData: false,
        data: form_data,                         
        type: 'post',
        success: function(php_script_response){
            alert(php_script_response); // display response from the PHP script, if any
        }
     });
});

Example 2: send parameters with file ajax jquery

$(function() {
    $('button[type=submit]').click(function (event) {
        event.preventDefault();

        // retrieve form element
        var form = this.form;
        // prepare data
        var data = new FormData(form);
        // get url
        var url = 'wherever.php';

        // send request
        $.ajax({
            type: 'POST',
            url: url,
            data: data,
            processData: false,
            contentType: false
        });
    });
});