2011-08-17 25 views
1

我有一個文件上傳表單。提交後,表單將數據發佈到iframe,然後使用jQuery和PECL UploadProgress獲取文件的上傳進度。當腳本請求上載進度並且響應爲空時,文件已經完成上傳,然後腳本讀取iframe以獲取文件的信息。我該如何解決我的jQuery上傳腳本不拋出錯誤?

腳本以響應爲空時清除的間隔運行。

我的腳本問題是,如果文件上傳之前請求文件進度已經發生,將會發生錯誤,因爲瀏覽器將嘗試獲取尚未設置的屬性值(迴應將是空白的)。

如何在腳本嘗試獲取尚未設置的屬性值之前檢查文件是否已完成上載?

是否有某種方法可以將某種偵聽器添加到iframe中,因此當它完成加載時,我可以立即取消間隔並顯示結果?

我曾想過在請求之前檢查iframe的內容,如果有數據,取消請求並顯示數據,但似乎不起作用。

錯誤我得到的IE:

Line: 37 
    Error: Unable to get value of the property 'filename': object is null or undefined 

道歉提前如果這似乎有點傻,我還沒有睡又在做這個項目,我無法找到一個方法來解決這個問題。

function beginUpload() 
{ 
    // Hide the upload form 
    $("#uploadContainer").hide(function(){ 
     $(".fileProgress").show(); 
    }); 

    var interval = setInterval(function(){ 

     $.getJSON("upload.php?uploadProgressKey=" + uploadProgressKey, function(returnData){ 

      if($("#uploadFrame").contents().find("html body").html() !== "") 
      { 
       clearInterval(interval); 
       $("#content").html($("#uploadFrame").contents().find("html body").html()); 
      } 
      if(returnData == null) 
      { 
       // Upload complete as there is not data to report about upload 
       // Stop the interval of checking the data 
       clearInterval(interval); 

       // Load result to content div 
       $("#content").html($("#uploadFrame").contents().find("html body").html()); 
      } 

      setUploadInformation(returnData.filename, returnData.bytes_uploaded, returnData.bytes_total, returnData.speed_average); 
      setUploadBar(returnData.bytes_uploaded, returnData.bytes_total); 
     }); 

    }, 800); 
} 
+1

顯示一些代碼。儘管您可能只需要爲進度檢查項目設置一個標誌,以檢查上傳是否真的在進行中。 –

+0

增加了一些代碼。 – Muggles

回答

2

不知道這是怎麼回事工作:

function beginUpload() { 
    // Hide the upload form 
    $("#uploadContainer").hide(function(){ 
     $(".fileProgress").show(); 
    }); 

    var checkProgress = function() { 
     $.getJSON("upload.php?uploadProgressKey=" + uploadProgressKey, function(returnData){ 

      if($("#uploadFrame").contents().find("html body").html() !== "") { 
       clearInterval(interval); 
       $("#content").html($("#uploadFrame").contents().find("html body").html()); 
      } 
      if(returnData == null) { 
       // Upload complete as there is not data to report about upload 
       // Stop the interval of checking the data 
       clearInterval(interval); 

       // Load result to content div 
       $("#content").html($("#uploadFrame").contents().find("html body").html()); 
      } 

      setUploadInformation(returnData.filename, returnData.bytes_uploaded, returnData.bytes_total, returnData.speed_average); 
      setUploadBar(returnData.bytes_uploaded, returnData.bytes_total); 
     }); 

    } 
    var interval = setInterval(checkProgress, 800); 
    checkProgress(); 
} 

你第一時間間隔開始後800毫秒。我創建了函數checkProgress並在初始化時間間隔後立即調用它。這樣你就可以開始全部檢查。

如果第一次AJAX調用(getJSON)在上傳完成後仍然可以遇到相同的錯誤?

+0

謝謝,我將在訪問Windows計算機時嘗試此代碼並報告結果。 – Muggles

+0

排序問題,謝謝。 – Muggles

相關問題