我有以下代码:
<td id="drop-files" ondragover="return false;" colspan="2">
<br />Drag and drop files to be uploaded...<br /><br />
<input type="hidden" id="iFile" value="" /><input type="hidden" id="numFiles" value="" />
<div id="progressBar" style="width: 300px; height: 20px; border-radius: 20px; border: 1px solid rgb(0,0,0); opacity: 0; text-align: center; background: -moz-linear-gradient(left, rgb(32,96,160) 0%, rgb(32,96,160) 0%, rgb(255,255,255) 0%, rgb(255,255,255) 100%);">0%</div><br />
<div id="fileCase"></div>
</td>
// Note: The double ## is not a mistake. The code is running under
// ColdFusion where # is a is reserved character. So ## escapes the #
$(document).ready(function() {
function pad(number, digits) {
return Array(Math.max(digits - String(number).length + 1, 0)).join(0) + number;
}
function processFile(file,num,arr){
var inputRow = "<input type='hidden' value='' id='file_"+pad(arr,3)+"' name='file_"+pad(arr,3)+"' />";
$("##fileCase").append(inputRow);
var o = $("##file_"+pad(arr,3));
var reader = new FileReader();
reader.onload = function(e) {
o.val(e.target.result.replace(/</g, "<").replace(/>/g, ">"));
}
reader.readAsDataURL(file);
}
function updateProgress(idx,tot){
var midPt = Math.floor((idx)/tot*100);
$("##progressBar").css("background","-moz-linear-gradient(left, rgb(32,96,160) 0%, rgb(32,96,160) "+midPt+"%, rgb(255,255,255) "+midPt+"%, rgb(255,255,255) 100%)");
$("##progressBar").html(midPt+"%");
}
// Sets up dataTransfer event
jQuery.event.props.push('dataTransfer');
// File Array
var nameArray = [];
$("##fileCase").append("<input type='hidden' id='nameArray' value='' name='nameArray'>");
// Bind drop to drop zone
$('##drop-files').bind('drop', function(e) {
var iFile = 0;
// Prevents element from default function
e.preventDefault();
// Grabs list of files
var files = e.dataTransfer.files;
var numFiles = files.length;set("numFiles",numFiles);
$("##progressBar").animate({opacity: 1},500);
// Loop through files...
updateProgress(0,numFiles);
$.each(files, function(index, file) {
nameArray.push(files[index].name);
arr = nameArray.length;
$("##nameArray").val(nameArray.join("|"));
processFile(files[index],index+1,arr);
iFile++;set("iFile",iFile);
//setTimeout("updateProgress("+iFile+","+numFiles+");",250);
setTimeout("var midPt = Math.floor(get('iFile')/get('numFiles')*100);$('##progressBar').css('background','-moz-linear-gradient(left, rgb(32,96,160) 0%, rgb(32,96,160) '+midPt+'%, rgb(255,255,255) '+midPt+'%, rgb(255,255,255) 100%)');$('##progressBar').html(midPt+'%');",500)
});
});
});
它工作得很好,除了一点点小东西。进度条似乎太快了 100%。我宁愿把它放慢一点,这样我就能得到一些看起来像是在彻底做某事的东西。
任何人都知道我可以如何放慢速度,以便我们可以真正看到进度条工作?
代码中的set()
和get()
函数只是我创建的快速函数,用于将变量存储在隐藏的输入表单元素中,以允许setTimeout
函数工作。
欣赏你的想法。