上传文件时,定时调用ajax读取缓存信息,如果有session存在,就要等到上传完成服务器才会响应ajax发出的请求,这是什么原因?

xiayun201225 2012-10-08 04:32:47
背景:上传文件时,想要在页面上显示上传进度。在页面中放了一个装有FileUpload的iframe,选择文件之后,提交iframe,上传文件,并在缓存里循环更新上传进度。同时定时调用ajax去读取缓存信息,更新到页面上。

问题:如果在session中有任何内容存在,则只有等到上传完成才会响应ajax发出的请求。如果session中没有内容,则可以正常显示。请教各位大侠,这是什么原因?

代码
主页面UploadPic.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UploadPic.aspx.cs" Inherits="UploadPic" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<script type="text/javascript">
var form;
var intervalID = 0;
function startUpload()
{
var msg = document.getElementById("msg");
var url = "progressmonitor.aspx";
var postStr = "guid=<%=myGuid %>";

//实例化Ajax
var ajax = false;
if(window.XMLHttpRequest)
{
ajax = new XMLHttpRequest();
if (ajax.overrideMimeType)
{
ajax.overrideMimeType("text/xml");
}
}
else if (window.ActiveXObject)
{
try
{
ajax = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
ajax = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!ajax)
{ // 异常,创建对象实例失败
window.alert("不能创建XMLHttpRequest对象实例.");
return false;
}


intervalID = window.setInterval(function(){
ajax.open("POST", url, true);
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.send(postStr); }, 500);

//获取执行状态
ajax.onreadystatechange = function()
{
//如果执行状态成功,那么就把返回信息写到指定的层里
if (ajax.readyState == 4 && ajax.status == 200)
{
msg.innerHTML = ajax.responseText;
}
}

form.submit();
}

function register(form){
this.form = form;
}

function onComplete(type){
// 自动消失
window.clearInterval(intervalID);
}
</script>
<body>
<div id="msg">
</div>
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="hfguid" runat="server" />
<iframe id="uploadFrame" frameborder="0" scrolling="no" src="upload.aspx"></iframe>
</div>
</form>
</body>
</html>


后台
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class UploadPic : System.Web.UI.Page
{
protected string myGuid;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["aa"] = 3;
myGuid = System.Guid.NewGuid().ToString();
this.hfguid.Value = myGuid;
}
}
}


内嵌的iframe:upload.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="fileUpload" runat="server" Width="100%" onchange="upload(this);" />
<asp:HiddenField ID="hfguid" runat="server" />
</div>
</form>
</body>
</html>
<script type="text/javascript">
window.onload = function() {
var myform = document.getElementById("form1");
window.parent.register(myform);
document.getElementById("hfguid").value = parent.document.getElementById("hfguid").value;
}

function upload(ob)
{
parent.startUpload();
}
</script>


后台
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Web.Caching;
using System.Threading;
using System.Web.UI.MobileControls;
using System.Collections.Generic;

public partial class upload : System.Web.UI.Page
{
private const int BUFFERSIZE = 5000;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
uploadFile(this.fileUpload.PostedFile);
}
}

protected void uploadFile(HttpPostedFile postedFile)
{
string filePath = @"C:\upload file\";
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}

string guid = this.hfguid.Value.ToString();

//object fileObj = HttpContext.Current.Cache[guid];
//if (fileObj != null)
//{
// HttpContext.Current.Cache.Remove(guid);
//}

List<FileInfo> infos = Cache[guid] as List<FileInfo>;
if (infos == null)
{
infos = new List<FileInfo>();
}

FileInfo info = new FileInfo();
info.FileName = postedFile.FileName;
info.TotalLength = postedFile.ContentLength;
info.CurrentLength = 0;
info.UploadPath = filePath;
infos.Add(info);

HttpContext.Current.Cache.Add(guid, infos, null, DateTime.Now.AddDays(1), TimeSpan.Zero, CacheItemPriority.AboveNormal, null);

FileStream fStream = new FileStream(filePath + postedFile.FileName, FileMode.Create);
Stream reader = postedFile.InputStream;
byte[] buffer = new byte[BUFFERSIZE];
int len = reader.Read(buffer, 0, BUFFERSIZE);

while (len > 0)
{
fStream.Write(buffer, 0, len);
info.CurrentLength += len;
//模拟延时用,实际应用的时候注销他
Thread.Sleep(100);
HttpContext.Current.Cache[guid] = infos;
len = reader.Read(buffer, 0, BUFFERSIZE);
}

reader.Close();
fStream.Close();

const string js = "window.parent.onComplete('success');";
ScriptManager.RegisterStartupScript(this, this.GetType(), "progress", js, true);
}

}


ajax请求:progressmonitor.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="progressmonitor.aspx.cs" Inherits="progressmonitor" %>


<table>
<tr>
<td>
图片名
</td>
<td style="width:200px;">
上传进度
</td>
</tr>

<%=uploadinfo%>
</table>



后台
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Collections.Generic;

public partial class progressmonitor : System.Web.UI.Page
{
protected string uploadinfo = string.Empty;

protected void Page_Load(object sender, EventArgs e)
{
string guid = Request.Params["guid"].ToString();
List<FileInfo> infos = HttpContext.Current.Cache[guid] as List<FileInfo>;
if (infos == null)
{
return;
}

for (int i = 0; i < infos.Count; i++)
{
int percent = (int)Math.Ceiling((double)infos[i].CurrentLength / (double)infos[i].TotalLength * 100);
uploadinfo += "<tr><td><div>" + infos[i].FileName + "</div></td><td><div style=\"width:" + percent + "%; background-color:Green;\"></div></td></tr>";
}
}
}
...全文
145 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiaht2009 2012-10-08
  • 打赏
  • 举报
回复
帮顶,学习
可不可以先清空session呢
# 主成分分析PCA降维算法Python实现 - 数据降维和特征提取 ## 项目简介 本项目提供了主成分分析(Principal Component Analysis, PCA)算法的完整Python实现,包含从数据预处理到结果可视化的全套功能。PCA是一种经典的无监督降维算法,通过线性变换将高维数据投影到低维空间,同保持数据的最大方差,广泛应用于数据降维、特征提取、数据可视化等领域。 ### 功能特点 - **完整的PCA算法实现**:包含数据标准化、协方差矩阵计算、特征值分解等核心步骤 - **多种数据生成器**:内置多种类型的测试数据生成功能 - **丰富的可视化功能**:支持解释方差图、散点图、双图、热力图等多种可视化方式 - **灵活的参数配置**:支持自定义主成分数量、标准化选项等 - **全面的分析工具**:包含特征重要性分析、重构误差计算、最优成分数量确定等 - **详细的示例代码**:提供多个应用场景的完整示例 ## 安装说明 安装依赖 ```bash pip install -r requirements.txt ``` ## 使用说明 ```bash # 运行所有演示 pytho ```bash # 运行所有演示 python main.py --demo all # 运行特定演示 python main.py --demo iris # 鸢尾花数据集分析 python main.py --demo high_dim # 高维数据降维 python main.py --demo correlated # 相关性数据分析 python main.py --demo comprehensive # 综合分析 # 指定输出目录 python main.py --output results ```
基于Javaweb的机房管理系统的设计与实现编程环境:idea2022平台,jdk1.8,tomcat8.5编程语言:java语言,编程技术:前端vue,div,css,后端:springboot框架,数据库:mysql5.7版本,Navicat Premium 12插件前台+后台前台学生注册,登录机房安全公告列表, 点击查看详情机房信息列表(点击搜索机房,查看到机房的详细情况)上机预约:选择机房,查看电脑信息,显示多少台电脑,点击可以查看机房的电脑详情,多少空闲的,点击可以预约电脑(填写预约间,预约人)机房消防检公布设备故障上报:注册的学生也可以进行设备故障报修后台管理员管理员信息管理机房安全公告管理机房消防检查管理(记录每次的消防检查,对检查结果进行公布)教师信息管理维修人员管理注册学生管理,审核机房信息管理(多个机房)设备信息管理(电脑设备)填写CPU,内存,品牌,显卡等基本信息日志信息管理教师教师资料修改机房上机预约管理软件设备报修,查看维修结果硬件设备报修,查看维修结果上机信息管理:设置上机信息,下机信息,查看上机间,查看到历史上机信息维修人员资料信息修改软件设备报修管理,填写维修结果硬件设备报修管理,填写维修结果设备故障报修管理:主要是针对学生前台提交的报修进行管理,处理学生学生资料修改我的预约信息我的上机:点击选择某个电脑进行上机,只能选择预约同意的电脑进行上机,我的下机:上机结束后,点击可以下机,并且计算出上机间我的历史上机我的设备故障报修管理

62,248

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧