简单实现asp.net进度条

程序中为了让更直观的反映命令执行的进度,考虑使用进度条,但是asp.net中没有专门的进度条控件,在网上搜了一下,实现方法都很复杂,就自己动手做了一个,实现起来其实也很简单。后来又想了个方法,加在后面。


方案一:适用webform

效果如图:


 用两层DIV,分别表示进度条总的外框和当前进度,通过不断调整“processbar”的DIv宽度,就实现了当前进度的显示

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
        function set() { //用来设置进度条的长度
            var obj,Mywidth;
            obj = document.getElementById("processbar");
            Mywidth = obj.style.width; Mywidth = Mywidth.replace("px", ""); Mywidth = parseInt(Mywidth);Mywidth++;
            obj.style.width = Mywidth + "px";
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div style="padding: 1px; background-color: #E6E6E6; border: 1px solid #C0C0C0; width: 300px; height: 20px;">
     <div id="processbar" style="background-color: #00CC00; height: 18px; width: 1px;">
      
    </div>
    </div>
    <asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>

后台代码部分

使用 ClientScript.RegisterStartupScript向前台代码中注册新的js脚本,但是需要注意的事 ClientScript.RegisterStartupScript(ClientScript.GetType(), key, str),中的key不变的话,词句只有第一次起作用,为了能够不断的刷新页面上的进度条,我们必须不断的改变key的值

 Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim i As Int16
        Dim str As String = ""
        For i = 0 To 100
            str = "<script>"

            str += "set();"
            str = str + "</script>"
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "set" & i, str)
          
        Next

    End Sub

方案二、适用于UI代码分离

思路:通过中间文件来实现,需要三个文件,前台文件 、

思路:后台程序把运行的实时状态写入session中,中间文件负责把当前session的状态输出,前台文件通过反复读取中间文件的输出,而获取到后台程序执行的进度,并显示,从而达到显示进度的作用,此方案在使用jquery嵌套调用时会有回调执行错误,具体代码随后加上。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
ASP.NET jQuery进度条上传是一种常见的网页上传文件功能。通过前端jQuery插件和后端ASP.NET技术,实现了文件上传过程中的进度条展示,提高了用户体验和操作效率。具体实现方式如下: 1. 前端实现 使用jQuery的ajaxForm插件,结合jQueryUI的progressbar插件,进行文件上传进度条的展示。在HTML页面上添加上传表单和进度条,通过ajaxForm设置表单提交事件,实现文件上传过程中进度条的实时更新。代码如下: ``` <form id="uploadForm" enctype="multipart/form-data" method="post" action="UploadFile.ashx"> <input type="file" name="fileUpload" /> <input type="submit" value="上传" /> </form> <div id="progressBar"></div> ``` ``` $("#uploadForm").ajaxForm({ beforeSend: function() { $("#progressBar").progressbar({ value: 0 }); }, uploadProgress: function(event, position, total, percentComplete) { $("#progressBar").progressbar({ value: percentComplete }); }, success: function() { $("#progressBar").progressbar({ value: 100 }); alert("上传成功!"); }, error: function() { alert("上传失败!"); } }); ``` 2. 后端实现ASP.NET中,通过HttpHandler实现文件上传处理,从request对象中获取上传的文件信息,根据文件大小和上传进度,实时更新进度条的值。代码如下: ``` <%@ WebHandler Language="C#" Class="UploadFile" %> using System; using System.Web; public class UploadFile : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; HttpPostedFile file = context.Request.Files["fileUpload"]; int fileSize = file.ContentLength; int bytesRead = 0; byte[] buffer = new byte[8192]; using (System.IO.Stream stream = file.InputStream) using (System.IO.BinaryReader reader = new System.IO.BinaryReader(stream)) using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { double percentComplete = 0; int totalRead = 0; while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, bytesRead); totalRead += bytesRead; percentComplete = (double)totalRead / fileSize * 100; context.Response.Write(percentComplete.ToString()); } } } public bool IsReusable { get { return false; } } } ``` 综上,ASP.NET jQuery进度条上传,通过前后端技术的组合应用,在网页上传过程中有效展示文件上传进度和结果,提升了用户的体验感和操作效率。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值