日期:2014-05-18  浏览次数:20490 次

IHttpHandler问题
因为我想在上传文件的时候显示进度,代码如下
C# code
using System;
using System.Data;
using System.Configuration;
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 OASystem.Class;
using System.IO;

namespace OASystem
{
    public class HttpUploadModule:IHttpModule
    {
        #region IHttpModule 成员

        void IHttpModule.Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(application_BeginRequest);
            context.EndRequest += new EventHandler(this.Application_EndRequest);
            context.Error += new EventHandler(this.Application_Error);
        }
        void IHttpModule.Dispose()
        {

        }
        #endregion

        public void application_BeginRequest(Object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            HttpWorkerRequest request = GetWorkerRequest(app.Context);
            System.Text.Encoding encoding = app.Context.Request.ContentEncoding;

            int bytesRead = 0; // 已读数据大小 
            int read;           // 当前读取的块的大小 
            int count = 8192;   // 分块大小 
            byte[] buffer;      // 保存所有上传的数据
            string uploadId; // 唯一标志当前上传的ID
            Progress progress; // 记录当前上传的进度信息
            int maxFileSize = 52428800;//最大上传大小50M

            if (request != null)
            {
                // 返回 HTTP 请求正文已被读取的部分。 
                byte[] tempBuff = request.GetPreloadedEntityBody(); //要上传的文件

                // 如果是附件上传 
                if (tempBuff != null && IsUploadRequest(app.Request))    //判断是不是附件上传 
                {
                    // 当前上传的ID,用来唯一标志当前的上传
                    // 用此UploadID,可以通过其他页面获取当前上传的进度
                    if (app.Context.Request.Cookies["UploadID"] == null)
                    {
                        HttpCookie hCookie = new HttpCookie("UploadID");
                        hCookie.Value = Guid.NewGuid().ToString();
                        app.Context.Response.Cookies.Add(hCookie);
                    }
                    uploadId = app.Context.Request.Cookies["UploadID"].Value.ToString();

                    // 获取上传大小 
                    // 
                    long length = long.Parse(request.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength)); 

                    // 开始记录当前上传状态
                    //                   
                    progress = new Progress(length, uploadId);
                    if (length <= maxFileSize)
                    {
                        //设置记录信息状态
                        progress.SetState(UploadState.ReceivingData);

                        buffer = new byte[length];
                        count = tempBuff.Length; // 分块大小

                        // 将已上传数据复制过去 
                        // 
                        Buffer.BlockCopy(tempBuff, //源数据 
                            0,                      //从0开始读 
                            buffer,                 //目标容器 
                            bytesRead,              //指定存储的开始位置 
                            count);                 //要复制的字节数。

                        // 开始记录已上传大小 
                        bytesRead = tempBuff.Length;
                        progress.SetBytesRead(bytesRead);
                        SetProgress(uploadId, progress, app.Application);

                        // 循环分块读取,直到所有数据读取结束 
                        while (request.IsClientConnected() && !request.IsEntireEntityBodyIsPreloaded() && bytesRead < length)
                        {
                            // 如果最后一块大小小于分块大小,则重新分块 
                            if (bytesRead + count > length)