我有一個文件上傳表單。提交後,表單將數據發佈到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);
}
顯示一些代碼。儘管您可能只需要爲進度檢查項目設置一個標誌,以檢查上傳是否真的在進行中。 –
增加了一些代碼。 – Muggles