Php CodeIgniter上载文件Ajax不工作

Php CodeIgniter上载文件Ajax不工作,php,codeigniter,Php,Codeigniter,无论我做什么,我似乎都无法通过AJAX将文件上传到CodeIgniter方法。它总是抛出并从未看到上载的文件 HTML 设置表单操作路径并将其用于ajax(最好不要使用onclick函数) 请尝试此代码 $('#upload').on('click', function() { var file_data = $('#foto1').prop('files')[0]; var form_data = new FormData();

无论我做什么,我似乎都无法通过AJAX将文件上传到CodeIgniter方法。它总是抛出并从未看到上载的文件

HTML


设置表单操作路径并将其用于ajax(最好不要使用onclick函数)

请尝试此代码

$('#upload').on('click', function() {
    var file_data = $('#foto1').prop('files')[0];   
    var form_data = new FormData();                  
    form_data.append('file', file_data);
    $.ajax({
            url: 'your_upload_php_file', // 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
            }
     });
});
Request Method:POST
Status Code:200 OK
    ------WebKitFormBoundaryvkN2BT8ZDxXmKj7Y
    Content-Disposition: form-data; name="userfile"; filename="clippy-windows-8-10.jpg"
    Content-Type: image/jpeg


function AJAX_upload_translation()
    {
        if (!isset($_FILES['userfile']['error']) || is_array($_FILES['userfile']['error'])) 
        {
            throw new RuntimeException('Invalid parameters.');
        }

    }
$(document).on("submit", ".myForm", function(event)
{
  event.preventDefault();
  $.ajax({
    url: $(this).attr("action"),//set form action url at your form
    type: $(this).attr("method"),//set form method at your form          
    data: new FormData(this),
    processData: false,
    contentType: false,
    success: function (data, status)
    {

    },
    error: function (xhr, desc, err)
    {


    }
  });
});
$('#upload').on('click', function() {
    var file_data = $('#foto1').prop('files')[0];   
    var form_data = new FormData();                  
    form_data.append('file', file_data);
    $.ajax({
            url: 'your_upload_php_file', // 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
            }
     });
});