Php ajax jquery文件上载不起作用

Php ajax jquery文件上载不起作用,php,jquery,file-upload,Php,Jquery,File Upload,我正在使用jQueryAjax上传文件。当我单击upload按钮时,它失败了,ajax的错误部分显示UncaughtTypeError:Cannotreadproperty'length'of undefined。我检查了代码,发现警告jqXHR表明第一次ajax调用成功,但submitForm中的ajax调用不起作用。控制器在$.eachevent之前停止,并在控制台中显示上述错误。请帮助我 我的代码如下: $(document).ready(function() { //for c

我正在使用jQueryAjax上传文件。当我单击upload按钮时,它失败了,ajax的错误部分显示UncaughtTypeError:Cannotreadproperty'length'of undefined。我检查了代码,发现警告jqXHR表明第一次ajax调用成功,但submitForm中的ajax调用不起作用。控制器在$.eachevent之前停止,并在控制台中显示上述错误。请帮助我

我的代码如下:

$(document).ready(function()
{ 
    //for checking whether the file queue contain files or not
    var files;
    // Add events
    $('input[type=file]').on('change', prepareUpload);

    // Grab the files 
    function prepareUpload(event)
    {
        files = event.target.files;
        alert(files);
    }
    $("#file-form").on('submit',uploadFiles);
    function uploadFiles(event)
    {
        event.stopPropagation();  
        event.preventDefault(); 

        // Create a formdata object and add the files
        var data = new FormData();
        $.each(files, function(key, value)
        {
            data.append(key, value);
            //alert(key+' '+ value);
        });
        $.ajax({
            url: 'module/portal/filesharing/upload.php?files',
            type: 'POST',
            data: data,
            cache: false,
            dataType: 'json',
            processData: false, 
            contentType: false, 
            success: function(data, textStatus, jqXHR)
            {
                if(typeof data.error === 'undefined')
                {
                    // Success so call function to process the form
                    submitForm(event, data);
                }
                else
                {
                    console.log('ERRORS: ' + data.error);
                }
            }
        });
        function submitForm(event, data)
        {
            // Create a jQuery object 
            $form = $(event.target);

            // Serialize the form data
            var formData = $form.serialize();//controller stops here

            // sterilise the file names
            $.each(data.files, function(key, value)
            {
                formData = formData + '&filenames[]=' + value;
            });

            $.ajax({
                url: 'update.php',
                type: 'POST',
                data: formData,
                cache: false,
                dataType: 'json',
                success: function(data, textStatus, jqXHR)
                {
                    if(typeof data.error === 'undefined')
                    {
                        // Success so call function to process the form
                        console.log('SUCCESS: ' + data.success);
                    }
                    else
                    {
                        // Handle errors here
                        console.log('ERRORS: ' + data.error);
                    }
                },
                error: function(jqXHR, textStatus, errorThrown)
                {
                    // Handle errors here
                    console.log('ERRORS: ' + textStatus);
                },
                complete: function()
                {
                    // STOP LOADING SPINNER
                }
            });
        }
    }  
});    
</script>

如果您希望它在跨浏览器上工作,我建议您像这样使用iframe

或者有一些jquery模块使用flash进行上传,它们也是旧版本internetexplorer的好选择

也许你的问题是这个,请检查一下

<form id='file-form' action="" method="post" enctype="multipart/form-data"> 
    <input type="file" name="file" id="filename" ><br>
    <input type="submit"  id='upload' value="Upload file">
</form>
$data = array();
if(isset($_GET['files']))
{    
    $error = false;
    $files = array();
    $uploaddir = 'module/portal/filesharing/upload/';
    foreach($_FILES as $file)
    {
        if(move_uploaded_file($file['tmp_name'], $uploaddir .basename($file['name'])))
        {
            $files[] = $uploaddir .$file['name'];
        }
        else
        {
            $error = true;
        }
    }
    $data = ($error) ? array('error' => 'There was an error uploading your files') :     array('files' => $files);
}
else
{
    $data = array('success' => 'Form was submitted', 'formData' => $_POST);
}

echo json_encode($data);