HttpUploadHandler.cs
上传用户:whjkdz
上传日期:2013-05-19
资源大小:79k
文件大小:6k
源码类别:

.net编程

开发平台:

C#

  1. #region License
  2. /*
  3. * SunriseUpload - Asp.net Upload Component
  4. *
  5. * Copyright (C) 2004 mic <mic4free@hotmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20. *
  21. * In case your copy of SunriseUpload does not include a copy of the license, you may find it online at 
  22. * http://www.gnu.org/copyleft/gpl.html
  23. *
  24. * You can find new release of this component at http://athena.9966.org/SunriseUpload .
  25. */
  26. #endregion
  27. using System;
  28. using System.Text;
  29. using System.Web;
  30. namespace Sunrise.Web.Upload
  31. {
  32. /// <summary>
  33. /// 
  34. /// </summary>
  35. public class HttpUploadHandler : IHttpHandler
  36. {
  37. /// <summary>
  38. /// Implement from IHttphanders
  39. /// </summary>
  40. public bool IsReusable
  41. {
  42. get { return true; }
  43. }
  44. public HttpUploadHandler()
  45. {
  46. }
  47. /// <summary>
  48. /// 
  49. /// </summary>
  50. /// <param name="uploadGUID"></param>
  51. public void InitProgress(string uploadGUID)
  52. {
  53. HttpContext context;
  54. UploadStatus uploadStatus = new UploadStatus(uploadGUID);
  55. //Load progress template into string buffer.
  56. StringBuilder sb = Utils.GetHtml("Progress.page");
  57. //While files were uploading, update the state.
  58. if (uploadStatus.IsActive)
  59. {
  60. switch (uploadStatus.State)
  61. {
  62. case UploadState.Initializing:
  63. {
  64. sb.Replace("${status}$", "初始化...");
  65. break;
  66. }
  67. case UploadState.Uploading:
  68. {
  69. sb.Replace("${status}$", "上传中...");
  70. break;
  71. }
  72. case UploadState.Uploaded:
  73. {
  74. sb.Replace("${status}$", "上传完成");
  75. break;
  76. }
  77. case UploadState.Moving:
  78. {
  79. sb.Replace("${status}$", "移动文件...");
  80. break;
  81. }
  82. case UploadState.Completed:
  83. {
  84. sb.Replace("${status}$", "完成");
  85. break;
  86. }
  87. }
  88. UpdateStatus(uploadStatus, sb, uploadGUID);
  89. }
  90. else
  91. {
  92. sb.Replace("${Script}$", "<script>window.opener=self;window.close();</script>");
  93. }
  94. context = Utils.GetContext();
  95. //Clear the cache of client browser.
  96. context.Response.Expires = 0;
  97. context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
  98. context.Response.ContentEncoding = Encoding.Default;
  99. context.Response.ContentType = "text/html";
  100. context.Response.Clear();
  101. //Show page to client.
  102. context.Response.Write(sb.ToString());
  103. }
  104. /// <summary>
  105. /// Update current upload status.
  106. /// </summary>
  107. /// <param name="uploadStatus"></param>
  108. /// <param name="builder"></param>
  109. /// <param name="uploadGUID"></param>
  110. private void UpdateStatus(UploadStatus uploadStatus, StringBuilder builder, string uploadGUID)
  111. {
  112. if (uploadStatus.State == UploadState.Uploading)
  113. {
  114. builder.Replace("${FileName}$", uploadStatus.FileName);
  115. builder.Replace("${UploadProgress}$", uploadStatus.Percent.ToString());
  116. builder.Replace("${SurplusProgress}$", Convert.ToString(100 - uploadStatus.Percent));
  117. builder.Replace("${UploadSpeed}$", (Utils.GetFormatString(uploadStatus.Speed) + "/s"));
  118. builder.Replace("${LeftTime}$", Utils.GetFormatString(uploadStatus.LeftTime));
  119. builder.Replace("${BtnOK}$", "disabled");
  120. builder.Replace("${Refresh}$", ("<meta http-equiv="Refresh" content="1";URL=progress.ashx?UploadID=" + uploadGUID + "">"));
  121. }
  122. else if (uploadStatus.State == UploadState.Completed)
  123. {
  124. builder.Replace("${UploadProgress}$", uploadStatus.Percent.ToString());
  125. builder.Replace("${SurplusProgress}$", Convert.ToString(100 - uploadStatus.Percent));
  126. builder.Replace("${FileName}$", (uploadStatus.FileCount.ToString() + "个文件上传成功!"));
  127. builder.Replace("${UploadSpeed}$", (Utils.GetFormatString(uploadStatus.Speed) + "/s"));
  128. builder.Replace("${LeftTime}$", "已完成,无时间剩余");
  129. uploadStatus.Dispose();
  130. builder.Replace("${BtnOK}$", "onclick="javascript:window.opener=self;window.close();return false;"");
  131. builder.Replace("${Refresh}$", "");
  132. }
  133. else
  134. {
  135. builder.Replace("${FileName}$", "读取中...");
  136. builder.Replace("${UploadProgress}$", "0");
  137. builder.Replace("${SurplusProgress}$", "100");
  138. builder.Replace("${UploadSpeed}$", (Utils.GetFormatString(uploadStatus.Speed) + "/s"));
  139. builder.Replace("${LeftTime}$", "0 秒");
  140. builder.Replace("${BtnOK}$", "disabled");
  141. builder.Replace("${Refresh}$", ("<meta http-equiv="Refresh" content="1";URL=progress.ashx?UploadID=" + uploadGUID + "">"));
  142. }
  143. if (uploadStatus.State == UploadState.Completed)
  144. {
  145. builder.Replace("${BtnCancel}$", "onclick="javascript:window.opener=self;window.close();return false;"");
  146. }
  147. else
  148. {
  149. if(Utils.IsAccordantBrowser())
  150. {
  151. builder.Replace("${BtnCancel}$", "onclick="javascript:dialogArguments.location.href=dialogArguments.location.href;window.close();"");
  152. }
  153. else
  154. {
  155. builder.Replace("${BtnCancel}$", "onclick="javascript:window.opener.opener=null;window.opener.location.href=window.opener.location.href;window.close();this.disabled=true;"");
  156. }
  157. }
  158. builder.Replace("${Script}$", "");
  159. }
  160. /// <summary>
  161. /// 
  162. /// </summary>
  163. /// <param name="context"></param>
  164. public void ProcessRequest(HttpContext context)
  165. {
  166. string uploadID = context.Request.QueryString["UploadID"];
  167. string filePath = context.Request.FilePath;
  168. filePath = filePath.Substring((filePath.LastIndexOf("/") + 1)).ToUpper();
  169. bool isUnknownRequest = false;
  170. if (filePath == "PROGRESS.ASHX")
  171. {
  172. this.InitProgress(uploadID);
  173. }
  174. else
  175. {
  176. isUnknownRequest = true;
  177. }
  178. if (isUnknownRequest)
  179. {
  180. throw new HttpException(500, "unknown request");
  181. }
  182. }
  183. }
  184. }