asp.net XMLHttpRequest 进度条以及lengthComputable always false的解决办法
一直用ajax好长时间了,对其原理也有一些了解,最近由于项目需要,使用ajax异步进度条的效果,就研究了一下,用原生的XMLHttpRequest实现进度条函数,XMLHttpRequest有以下函数可以使用,摘自(https://www.w3.org/TR/progress-events/)
type attribute value |
Description | Times | When |
|---|---|---|---|
loadstart |
Progress has begun. | Once. | First. |
progress |
In progress. | Zero or more. | After loadstart has been dispatched. |
error |
Progression failed. | Zero or once. | After the last progress has been dispatched, or after loadstart has been dispatched if progress has not been dispatched. |
abort |
Progression is terminated. | Zero or once. | |
load |
Progression is successful. | Zero or once. | |
loadend |
Progress has stopped. | Once. | After one of error, abort, or load has been dispatched. |
进度条函数主要使用progress事件。下面构造一个进度条实现的demo
1、构建页面代码
<div class="progress">
<div id="pros" class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="" aria-valuemin="" aria-valuemax="" style="width: 0%;">
</div>
</div>
<button id="trigger_ajax" type="button">请求数据</button>
<script type="text/javascript">
var trigger = document.getElementById("trigger_ajax");
trigger.onclick = function () {
var xhr = new XMLHttpRequest();
xhr.onprogress = function (event) {
console.log(event.lengthComputable);
console.log(event.loaded);
if (event.lengthComputable) {
var loaded = parseInt(event.loaded / event.total * ) + "%";
$('#pros').width(loaded);
$('#pros').text(loaded);
}
}
xhr.open("post", "/Home/aaa", true);
xhr.send(null);
}
</script>
进度条Html代码
2、后台处理接口
[HttpPost]
public void aaa()
{
string result = string.Empty;
for (int i = ; i <= ; i++)
{
result += i.ToString();
int len = result.Length;
Response.Headers.Add("Content-Length", len.ToString());
Response.Headers.Add("Content-Encoding", "UTF-8");
Response.Write(result);
}
}
后台数据处理接口
注意到
Response.Headers.Add("Content-Length", len.ToString());
Response.Headers.Add("Content-Encoding", "UTF-8");
,写出 http 头时候,附加 “Content-Length”和Content-Encoding,这样 JS 端的 progress 事件的 event.lengthComputable 值才会为 true, event.total 才会在数据传输完毕之前取得值,否则 event.lengthComputable 值会返回 false, event.total 在数据完成之前值都是0。

asp.net XMLHttpRequest 进度条以及lengthComputable always false的解决办法的更多相关文章
- 宏基4752g 开机进度条卡到75%左右,解决办法
起因:更新win10推送的更新补丁,失败自动回退.开机进度条只能走到75%,bios进不去,最后就卡在开机的logo.(还有其他人是win7直接升级win10,也出现了这种情况.)解决办法:重刷bio ...
- Eclipse进度条出现“Remote System Explorer Operation”解决方法
Eclipse进度条出现“Remote System Explorer Operation”解决方法
- 同一个ASP.NET页面放置多个UpdatePanel分别刷新的解决办法。.
原文:同一个ASP.NET页面放置多个UpdatePanel分别刷新的解决办法.. ScriptManager添加EnablePartialRending属性 <asp:ScriptManage ...
- ASP.NET实现进度条效果【转】
原文地址:http://www.jb51.net/article/115310.htm 这篇文章主要为大家详细介绍了ASP.NET实现简单的进度条效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一 ...
- ASP.NET中进度条的简单应用
<html xmlns="http://www.w3.org/1999/xhtml" id="mainWindow"> <head> & ...
- CentOS开机卡在进度条,无法正常开机的排查办法
CentOS开机的时候卡在进度条一直进不去 重启,按f5键进度条/命令行界面方式切换,确认卡问题后处理就好 我这边卡在redis服务,设置为开机启动但是一直服务启动不起来 重启按住"e&qu ...
- Eclipse里编辑代码,进度条出现“Remote System Explorer Operation”解决方法
Eclipse里编辑代码,进度条出现"Remote System Explorer Operation",导致Eclipse有卡顿. 解决方法: Eclipse -> Pre ...
- iis运行asp.net页面提示“服务器应用程序不可用”的解决办法_.NET.
原因:主要是iis安装在了net framwork之后 解决办法:需要在IIS中重新注册.net 也就是要用到系统盘: cd c:\windows\microsoft.net\framework\v2 ...
- ASP.NET项目与IE10、IE11不兼容的解决办法
1.解决办法 机器级别修复, 服务器所有ASP.NET程序受益 需要去微软下载对应asp.NET版本的修补程序 .NET 4 -http://support.microsoft.com/kb/2600 ...
随机推荐
- [转][Starling] 神器——原生Swf一键导出到Starling!
Swf一键导出到Starling中的工具,在Starling使用原生的MovieClip 来自:http://zmliu.github.io/2013/11/09/StarlingSwfTool/ 如 ...
- 如何解决google ping不通的问题。
1. 用http://ping.chinaz.com/ ping google的域名. 2. 会ping出很多ip,但是chinaz用的是测试网点去ping的,不是你本地宽带, 所以把ping出ip拷 ...
- oracle PL/SQL高级特性
触发器:存放在数据库中,并被隐含执行的存储过程. 由触发事件,触发条件,触发操作组成. DML触发器:指定触发器时机(before or after),触发事件(insert , delete, u ...
- Offline.js - 自动判断网络连接状态并提醒用户
http://www.cnblogs.com/lhb25/p/offline-js-alert-users-when-no-internet-connectivity.html 使用 jslint/j ...
- 《Linux内核设计与实现》读书笔记 第五章 系统调用
第五章系统调用 系统调用是用户进程与内核进行交互的接口.为了保护系统稳定可靠,避免应用程序恣意忘形. 5.1与内核通信 系统调用在用户空间进程和硬件设备间添加了一个中间层, 作用:为用户空间提供了一种 ...
- android删除无用资源文件的python脚本
随着android项目的进行,如果没有及时删除无用的资源时安装包会越来越大,是时候整理一下废弃资源缩小压缩包了,少年! 其实判断一个资源(drawable,layout)是否没有被使用很简单,文件名( ...
- Android中 Bitmap和Drawable相互转换的方法
1.Drawable->Bitmap Resources res=getResources(); Bitmap bmp=BitmapFactory.decodeResource(res, R.d ...
- [翻译]类型双关不好玩:C中使用指针重新解释是坏的
原文地址 Type punning isn't funny: Using pointers to recast in C is bad. C语言中一个重新解释(reinterpret)数据类型的技巧有 ...
- Java 获取汉字拼音的方法
package lius.util; import java.io.Serializable; import java.util.ArrayList; public class JString ...
- QStriingList
#include <QCoreApplication> #include<QDebug> #include<QStringList> int main(int ar ...