Пример #1
0
        private void Application_BeginRequest(Object sender, EventArgs e)
        {
            HttpApplication   app      = sender as HttpApplication;
            HttpWorkerRequest request  = GetWorkerRequest(app.Context);
            Encoding          encoding = app.Context.Request.ContentEncoding;

            int bytesRead = 0; // 已读数据大小
            int read;          // 当前读取的块的大小
            int count = 8192;  // 分块大小

            byte[]   buffer;   // 保存所有上传的数据
            string   uploadId; // 唯一标志当前上传的ID
            Progress progress; // 记录当前上传的进度信息

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

                // 如果是附件上传
                //
                if (
                    tempBuff != null &&
                    IsUploadRequest(app.Request)
                    )
                {
                    // 获取上传大小
                    //
                    long length = long.Parse(request.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength));
                    // 当前上传的ID,用来唯一标志当前的上传
                    // 用此UploadID,可以通过其他页面获取当前上传的进度
                    //
                    uploadId = app.Context.Request.QueryString["UploadID"];

                    // 开始记录当前上传状态
                    //
                    progress = new Progress(length, uploadId);
                    progress.SetState(UploadState.ReceivingData);

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

                    // 将已上传数据复制过去
                    //
                    Buffer.BlockCopy(tempBuff, 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)
                        {
                            count    = (int)(length - bytesRead);
                            tempBuff = new byte[count];
                        }

                        // 分块读取
                        //
                        read = request.ReadEntityBody(tempBuff, count);

                        // 复制已读数据块
                        //
                        Buffer.BlockCopy(tempBuff, 0, buffer, bytesRead, read);

                        // 记录已上传大小
                        //
                        bytesRead += read;
                        progress.SetBytesRead(bytesRead);
                        SetProgress(uploadId, progress, app.Application);
                    }
                    if (
                        request.IsClientConnected() &&
                        !request.IsEntireEntityBodyIsPreloaded()
                        )
                    {
                        // 传入已上传完的数据
                        //
                        InjectTextParts(request, buffer);

                        // 表示上传已结束
                        //
                        progress.SetBytesRead(bytesRead);
                        progress.SetState(UploadState.Complete);
                        SetProgress(uploadId, progress, app.Application);
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// 根据UploadID获取上传进度信息
        /// </summary>
        /// <param name="uploadId"></param>
        /// <param name="application"></param>
        /// <returns></returns>
        public static Progress GetProgress(string uploadId, HttpApplicationState application)
        {
            Progress progress = application["OpenlabUpload_" + uploadId] as Progress;

            return(progress);
        }