Jquery和BigFileUpload实现大文件上传及进度条显示
网络编程 2021-07-04 20:00www.168986.cn编程入门
这篇文章主要介绍了Jquery和BigFileUpload实现大文件上传及进度条显示的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
实现方法用到了高山来客 的bigfileupload组件,用高山来客的方法,弹出一个模式窗口,然后不停刷新获取进度,始终觉得体验感不好,于是想到用jquery来实现无刷新进度显示,因为提交页面后, 不能让其刷新页面,而是要不断地通过ajax获取progress.aspx返回的进度信息,所以用到了jquery.form的ajaxform提交。ajaxform提交后如果执行提交后的事件,比如在数据库里插入记录,还在调试中。
1、用到了jquery 的 progressbar 、form、MultFile及相关组件
<script src="../../mon/js/jquery-1.2.6.pack.js" type="text/javascript"></script> <script src="../../mon/jqControl/packed/ui.core.packed.js" type="text/javascript"></script> <!--FORM--> <script src="../../mon/jqControl/ajax/jquery.form.js" type="text/javascript"></script> <!--上传文件--> <script src="../../mon/jqControl/upLoad/jquery.MultiFile.pack.js" type="text/javascript"></script> <!--进度条--> <script src="../../mon/jqControl/packed/ui.progressbar.packed.js" type="text/javascript"></script> <!--对话框--> <!--我的JS --> <link href="../../mon/Css/jquery-ui-themeroller.css" rel="stylesheet" type="text/css" /> <script src="../gadget/JS/uploadfile.js" type="text/javascript">
2.uploadfile.js代码如下
$(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="dialogsu" > '; $("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, suess: function(data) { //$("#dialogsu").text(data); //sProgress(); if(data.su==1) { / $("#dialogsu").show(); $("#dialogsu").dialog( { modal: true, overlay: { opacity: 0.5, background: "black" } }); / } }, error:function(err) { alert(err); } }); }); }); function getProgress(){ $.ajax({ type: "post", dataType:"json", url: "uploadProgress.aspx", data: "UploadID=" + $("#UploadID").val(), suess: 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.su==-1){ alert(data.errmsg); } if (data.su==0){ setTimeout("getProgress()", 1000); } if (data.su==1){ return; } }, error:function(msg) { alert(msg); } }); } function startProgress(){ $("#uploadinfo").show(); setTimeout("getProgress()", 500); } function sProgress() { $("#uploadinfo").hide(); }
3:progress.aspx代码如下:
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) { //throw err; Response.Write("{'info':'" + err.Message.Replace("'", "") + "','su':-1}"); } } private string responeJSON(UploadContext upload, string su,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 += ",'su':" + su; s += ",'errmsg':'" + errmsg +"'"; return s; }
4.ajaxForm执行后,能够正常运行,那上传文件后,我如何执行其它操作,研究完了再发
以上所述是长沙网络推广给大家介绍的Jquery和BigFileUpload实现大文件上传及进度条显示,希望对大家有所帮助,如果大家有任何疑问请给我留言,长沙网络推广会及时回复大家的。在此也非常感谢大家对狼蚁SEO网站的支持!
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程