61,654
社区成员




<%@ 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;
}
}
}
<%@ 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);
}
}
<%@ 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>";
}
}
}