0

我为它制作了一个上传页面和 php 脚本,它是一个 AJAX 脚本,但是我在 Firebug 中遇到了一些错误,并且我的上传百分比没有被返回,到目前为止,我只能在 firefox 中进行一次上传,并且脚本赢了甚至不能在 chrome 中工作。

Firebug 中的错误

这是我的代码:

<?php 
    session_start();
    include ('../Connect/Connect.php');
    $User = $_SESSION['User'];

    if(isset($User))
    {
    }
    else 
    {
        header("location:../");
    }
    $Connect -> close();
?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <link rel="stylesheet" href="../StyleSheets/StyleSheet.css" />
        <link rel="stylesheet" href="../JQueryUI/css/black-tie/jquery-ui-1.10.1.custom.min.css" />
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>TeraShare</title>
        <script type="text/javascript" src="../JQuery/JQuery.js"></script>
        <script type="text/javascript" src="../JQueryUI/js/jquery-ui-1.10.1.custom.min.js"></script>
        <script type="text/javascript" src="../Scripts/JavaScript.js"></script>
        <script type="text/javascript">
            $(document).ready(function()
            {   
                var UploadB = $('#Submit');

                UploadB.click(function(event)
                {
                    event.preventDefault();
                    event.stopPropagation();

                    var Data = new FormData();

                    var Files = document.getElementById('Files');

                    for(var I = 0; I < Files.files.length; ++I)
                    {
                        var FilesName = Files.files[I].name;

                        Data.append('File[]', Files.files[I]);
                    }

                    var Request = XMLHttpRequest();

                    Request.upload.addEventListener('progress', function(event)
                    {
                        if(event.lengthComputable)
                        {
                            var Percent = event.loaded / event.total;
                            Progress = document.getElementById('#Progress');

                            while(Progress.hasChildNodes())
                            {
                                Progress.removeChild(Progress.firstChild);  
                            }

                            Progress.appendChild(document.createTextNode(Math.round(Percent * 100) + '%'));
                        }
                    });

                    Request.upload.addEventListener('load', function(event)
                    {
                        Progress.style.display = 'none';
                    });

                    Request.upload.addEventListener('error', function(event)
                    {
                        alert('Upload Failed.');
                    });

                    Request.open('POST', 'Upload.php');
                    Request.setRequestHeader('Cache-Control', 'no-cache');
                    Progress.style.display = 'block';
                    Request.send(Data);
                });
            });
        </script>
    </head>

    <body>
        <div id="Wrapper">
            <div id="Menu">
                <div id="Logo"><a href=""><img src="../Pictures/Logo.png" /></a></div>
                <div id="Buttons">
                    <a href="../LogOut/"><div class="Button" title="LogOut">LogOut</div></a>
                    <div class="Button" id="Upload" title="Upload">Upload</div>
                    <div class="Button" id="CFolder" title="Upload">Create Folder</div>
                    <div class="Button" id="Store" title="Store">Store</div>
                    <div class="Button" title="Menu"><?php echo $User; ?>&nbsp;<span class="Triangle"></span></div>
                </div>
            </div>
            <div id="Content">
                <div id="Fix">


                    <div id="UForm">
                        <form action="" method="post" enctype="multipart/form-data">
                            <input type="file" id="Files" name="File[]" multiple="multiple" />
                            <input type="submit" name="Submit" class="AB" id="Submit" value="Upload!" />
                            <div id="Progress"></div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>

php脚本:

    <?php
session_start();
include('../Connect/Connect.php');

$User = $_SESSION['User'];
$Token = $_POST['Token'];
$Files = $_FILES['File'];
if(isset($User))
{
    if(!empty($Files))
    {
        for($X = 0; $X < count($Files['name']); $X++)
        {
            $Name = $Files['name'][$X];
            $TMP = $Files['tmp_name'][$X];

            move_uploaded_file($TMP, '../Users/HARAJLI98/' . $Name);
        }
    }
}
else
{
    header("location:../"); 
}

$Connect->close();
?>
4

1 回答 1

1

Document.getElementById需要ID的物理字符串,你不需要指定它是一个ID。

您正在混淆 javascript 和 jQuery。在 jQuery 中,它$.("#Progress")会起作用,因为该$函数不知道您是在寻找一个 ID 还是一个类。

这个:

Progress = document.getElementById('#Progress');

应该是这样的:

Progress = document.getElementById('Progress');

或这个:

Progress = $("#Progress");
于 2013-02-23T20:06:15.823 回答