上传文件时,定时调用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>";
}
}
}
...全文
126 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiaht2009 2012-10-08
  • 打赏
  • 举报
回复
帮顶,学习
可不可以先清空session呢

61,654

社区成员

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

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

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

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