音频文件没有上传ajax

时间:2016-05-01 16:17:17

标签: javascript php jquery ajax audio

我尝试使用以下代码上传文件而不按提交按钮。它适用于不适用于音频或视频的图像。上传音频文件时,它会在日志中显示未定义的索引“文件”。

<script>
$('#headerimage').on('change', function() {
var form = document.getElementById('fileupload');
            var fileInput = document.getElementById('headerimage');
            var file = fileInput.files[0];
            var formData = new FormData();
            var filename = '';
            formData.append('file', file);
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    filaname = xhr.responseText;
					console.log(xhr.responseText);
                }
            }
            // Add any event handlers here...
            xhr.open('POST', form.getAttribute('action'), true);
            xhr.send(formData);
			});
</script>
<form id="fileupload" method="post" enctype="multipart/form-data" action="upload2.php">
     <input type="file" id="headerimage" spellcheck="true" class="typography" name="headerimage">
</form>

<?php
if(!empty($_FILES)) 
{
    $file = $_FILES;
    if($file['file']['error'] == 0) 
    {
        $name = explode('.', $file['file']['name']);
        $newName = $name[count($name)-1];
        if (move_uploaded_file($file['file']['tmp_name'], "uploads/".$newName)) 
        {
            echo $newName;
            exit;
        }
    } else {
        echo "err";
        exit;
    }
} else {
    echo "errrror";
    exit;
}
?>

0 个答案:

没有答案