2013-02-23 116 views
0

我爲它做了一個上傳頁面和php腳本,這是一個AJAX腳本,但我在Firebug中收到了一些錯誤,並且我的上傳百分比沒有被返回,到目前爲止我只能在firefox上做一次上傳腳本甚至不會在Chrome中工作。Javascript錯誤?對象爲空

Errors in 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(); 
?> 
+0

也許嘗試'VAR進展=的document.getElementById( '#進展');'添加var關鍵字? – 2013-02-23 19:56:18

+0

沒有奏效。 – 2013-02-23 19:59:35

+0

爲什麼給你使用jQuery你是否在'$ .ajax'上使用'XMLHttpRequest'? – Lloyd 2013-02-23 19:59:46

回答

1

​​需要ID的物理串,你並不需要指定它是一個ID。

您正在混合使用JavaScript和jQuery。在jQuery中,$.("#Progress")可以工作,因爲$函數不知道您正在查找的是一個ID還是一個類。

此:

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

應該是這樣的:

Progress = document.getElementById('Progress'); 

或本:

Progress = $("#Progress"); 
+0

我這樣做,我沒有在螢火蟲錯誤,但鉻不會工作。 – 2013-02-23 20:07:19

+0

你是什麼意思「它不工作」? – 2013-02-23 20:07:34

+0

它不會在Chrome中執行,但在Firefox中,除了在兩個瀏覽器中,它都不會出現問題,我仍然沒有得到百分比。 – 2013-02-23 20:08:40

相關問題