HttpUploadHandler.cs
上传用户:whjkdz
上传日期:2013-05-19
资源大小:79k
文件大小:6k
- #region License
- /*
- * SunriseUpload - Asp.net Upload Component
- *
- * Copyright (C) 2004 mic <mic4free@hotmail.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * In case your copy of SunriseUpload does not include a copy of the license, you may find it online at
- * http://www.gnu.org/copyleft/gpl.html
- *
- * You can find new release of this component at http://athena.9966.org/SunriseUpload .
- */
- #endregion
- using System;
- using System.Text;
- using System.Web;
- namespace Sunrise.Web.Upload
- {
- /// <summary>
- ///
- /// </summary>
- public class HttpUploadHandler : IHttpHandler
- {
- /// <summary>
- /// Implement from IHttphanders
- /// </summary>
- public bool IsReusable
- {
- get { return true; }
- }
- public HttpUploadHandler()
- {
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="uploadGUID"></param>
- public void InitProgress(string uploadGUID)
- {
- HttpContext context;
- UploadStatus uploadStatus = new UploadStatus(uploadGUID);
- //Load progress template into string buffer.
- StringBuilder sb = Utils.GetHtml("Progress.page");
- //While files were uploading, update the state.
- if (uploadStatus.IsActive)
- {
- switch (uploadStatus.State)
- {
- case UploadState.Initializing:
- {
- sb.Replace("${status}$", "初始化...");
- break;
- }
- case UploadState.Uploading:
- {
- sb.Replace("${status}$", "上传中...");
- break;
- }
- case UploadState.Uploaded:
- {
- sb.Replace("${status}$", "上传完成");
- break;
- }
- case UploadState.Moving:
- {
- sb.Replace("${status}$", "移动文件...");
- break;
- }
- case UploadState.Completed:
- {
- sb.Replace("${status}$", "完成");
- break;
- }
- }
- UpdateStatus(uploadStatus, sb, uploadGUID);
- }
- else
- {
- sb.Replace("${Script}$", "<script>window.opener=self;window.close();</script>");
- }
- context = Utils.GetContext();
- //Clear the cache of client browser.
- context.Response.Expires = 0;
- context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
- context.Response.ContentEncoding = Encoding.Default;
- context.Response.ContentType = "text/html";
- context.Response.Clear();
- //Show page to client.
- context.Response.Write(sb.ToString());
- }
- /// <summary>
- /// Update current upload status.
- /// </summary>
- /// <param name="uploadStatus"></param>
- /// <param name="builder"></param>
- /// <param name="uploadGUID"></param>
- private void UpdateStatus(UploadStatus uploadStatus, StringBuilder builder, string uploadGUID)
- {
- if (uploadStatus.State == UploadState.Uploading)
- {
- builder.Replace("${FileName}$", uploadStatus.FileName);
- builder.Replace("${UploadProgress}$", uploadStatus.Percent.ToString());
- builder.Replace("${SurplusProgress}$", Convert.ToString(100 - uploadStatus.Percent));
- builder.Replace("${UploadSpeed}$", (Utils.GetFormatString(uploadStatus.Speed) + "/s"));
- builder.Replace("${LeftTime}$", Utils.GetFormatString(uploadStatus.LeftTime));
- builder.Replace("${BtnOK}$", "disabled");
- builder.Replace("${Refresh}$", ("<meta http-equiv="Refresh" content="1";URL=progress.ashx?UploadID=" + uploadGUID + "">"));
- }
- else if (uploadStatus.State == UploadState.Completed)
- {
- builder.Replace("${UploadProgress}$", uploadStatus.Percent.ToString());
- builder.Replace("${SurplusProgress}$", Convert.ToString(100 - uploadStatus.Percent));
- builder.Replace("${FileName}$", (uploadStatus.FileCount.ToString() + "个文件上传成功!"));
- builder.Replace("${UploadSpeed}$", (Utils.GetFormatString(uploadStatus.Speed) + "/s"));
- builder.Replace("${LeftTime}$", "已完成,无时间剩余");
- uploadStatus.Dispose();
- builder.Replace("${BtnOK}$", "onclick="javascript:window.opener=self;window.close();return false;"");
- builder.Replace("${Refresh}$", "");
- }
- else
- {
- builder.Replace("${FileName}$", "读取中...");
- builder.Replace("${UploadProgress}$", "0");
- builder.Replace("${SurplusProgress}$", "100");
- builder.Replace("${UploadSpeed}$", (Utils.GetFormatString(uploadStatus.Speed) + "/s"));
- builder.Replace("${LeftTime}$", "0 秒");
- builder.Replace("${BtnOK}$", "disabled");
- builder.Replace("${Refresh}$", ("<meta http-equiv="Refresh" content="1";URL=progress.ashx?UploadID=" + uploadGUID + "">"));
- }
- if (uploadStatus.State == UploadState.Completed)
- {
- builder.Replace("${BtnCancel}$", "onclick="javascript:window.opener=self;window.close();return false;"");
- }
- else
- {
- if(Utils.IsAccordantBrowser())
- {
- builder.Replace("${BtnCancel}$", "onclick="javascript:dialogArguments.location.href=dialogArguments.location.href;window.close();"");
- }
- else
- {
- builder.Replace("${BtnCancel}$", "onclick="javascript:window.opener.opener=null;window.opener.location.href=window.opener.location.href;window.close();this.disabled=true;"");
- }
- }
- builder.Replace("${Script}$", "");
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="context"></param>
- public void ProcessRequest(HttpContext context)
- {
- string uploadID = context.Request.QueryString["UploadID"];
- string filePath = context.Request.FilePath;
- filePath = filePath.Substring((filePath.LastIndexOf("/") + 1)).ToUpper();
- bool isUnknownRequest = false;
- if (filePath == "PROGRESS.ASHX")
- {
- this.InitProgress(uploadID);
- }
- else
- {
- isUnknownRequest = true;
- }
- if (isUnknownRequest)
- {
- throw new HttpException(500, "unknown request");
- }
- }
- }
- }