本文介紹如何結合高山來客的 BigFileUpload 組件與 jQuery,實現大文件上傳並無刷新地顯示實時進度。相比彈窗反覆刷新的方式,透過 ajax 定時獲取進度信息的體驗要順暢許多。
本例用到了 jQuery 的 progressbar、form、MultiFile 等相關組件:
<script src="../../common/js/jquery-1.2.6.pack.js" type="text/javascript"></script> <script src="../../common/jqControl/packed/ui.core.packed.js" type="text/javascript"></script> <!--FORM--> <script src="../../common/jqControl/ajax/jquery.form.js" type="text/javascript"></script> <!--上傳文件--> <script src="../../common/jqControl/upLoad/jquery.MultiFile.pack.js" type="text/javascript"></script> <!--進度條--> <script src="../../common/jqControl/packed/ui.progressbar.packed.js" type="text/javascript"></script> <link href="../../common/Css/jquery-ui-themeroller.css" rel="stylesheet" type="text/css" /> <script src="../gadget/JS/uploadfile.js" type="text/javascript">
$(function() {
var info = '<div style="display:none" id="uploadinfo">';
info += '<p>正在上傳: <span id="uploadfilename"></span></p>';
info += '<p>上傳速度: <span id="uploadrate"></span> </p>';
info += '<p>狀態: <span id="uploadtitle"></span></p>';
info += '<p>預計剩余時間: <span id="uploadlefttime"></span></p>';
info += '<p>上傳文件大小: <span id="uploadtotal"></span></p>';
info += '<p>已上傳大小: <span id="uploadcurrent"></span></p>';
info += '<p><div id="uploadprogressbar"></div></p>';
info += '</div><div id="dialogsucc" > ';
$("body").append(info);
//進度條
$("#uploadprogressbar").progressbar();
//上傳
$('#fileupload').MultiFile();
$('#btshow').click(function() {
alert($("body").html());
});
//提交
$('#btnsubmit').click(function() {
$("#uploadForm").ajaxSubmit(
{
dataType: 'text',
beforeSubmit: function(a,f,o) {
startProgress();
},
async:false,
success: function(data) {
if(data.succ==1) {
}
},
error:function(err) {
alert(err);
}
});
});
});
function getProgress(){
$.ajax({
type: "post",
dataType:"json",
url: "uploadProgress.aspx",
data: "UploadID=" + $("#UploadID").val(),
success: function(data){
$("#uploadprogressbar").progressbar("progress", data.percent);
$("#uploadfilename").html(data.filename);
$("#uploadrate").html(data.rate);
$("#uploadtitle").html(data.info);
$("#uploadlefttime").html(data.lefttime);
$("#uploadtotal").html(data.total);
$("#uploadcurrent").html(data.current);
if(data.succ==-1){ alert(data.errmsg); }
if (data.succ==0){ setTimeout("getProgress()", 1000); }
if (data.succ==1){ return; }
},
error:function(msg) {
alert(msg);
}
});
}
function startProgress(){
$("#uploadinfo").show();
setTimeout("getProgress()", 500);
}
function stopProgress() {
$("#uploadinfo").hide();
}
後台定時返回當前上傳進度,供前端 ajax 拉取:
protected void Page_Load(object sender, EventArgs e) {
try {
string s = "{";
UploadContext upload = UploadContextFactory.GetUploadContext(Request["UploadID"]);
//初始化
if (upload.Status == uploadStatus.Initializing) {
s += responeJSON(upload, "0", "");
}
if (upload.Status == uploadStatus.Uploading) {
s += responeJSON(upload, "0", "");
}
if (upload.Status == uploadStatus.Complete) {
s += responeJSON(upload, "1", "");
}
if (upload.Status == uploadStatus.HasError) {
s += responeJSON(upload, "-1", "");
}
s += "}";
Response.Write(s);
} catch (Exception err) {
Response.Write("{'info':'" + err.Message.Replace("'", "") + "','succ':-1}");
}
}
private string responeJSON(UploadContext upload, string succ,string errmsg) {
string s = "";
s += "'info':'" + upload.FormatStatus + "'" ;
s += ",'percent':'" + Convert.ToInt32((upload.Readedlength * 100.0 / upload.TotalLength)).ToString() + "'";
s += ",'current':'" + (upload.Readedlength/1024).ToString() + "KB'";
s += ",'total':'" + (upload.TotalLength/1024).ToString() + "KB'";
s += ",'rate':'" + upload.FormatRatio + "'";
s += ",'filename':'" + upload.CurrentFile + "'";
s += ",'cancel_upload':" + 0 ;
s += ",'lefttime':'" + upload.LeftTime + "'";
s += ",'succ':" + succ;
s += ",'errmsg':'" + errmsg +"'";
return s;
}
透過 ajaxForm 提交後,再定時調用 getProgress() 拉取後台進度,即可在頁面上無刷新地更新進度條、上傳速度與剩餘時間等信息,从而大幅提升大文件上傳的操作体验。