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

.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. namespace Sunrise.Web.Upload
  29. {
  30. /// <summary>
  31. /// Enum of upload state.
  32. /// </summary>
  33. public enum UploadState : byte
  34. {
  35. Error = 5,
  36. Completed = 4,
  37. Initializing = 0,
  38. Moving = 3,
  39. Uploaded = 2,
  40. Uploading = 1
  41. }
  42. public class UploadStatus : IDisposable
  43. {
  44. #region Fields
  45. private DateTime beginTime;
  46. private int fileCount;
  47. private long fileLength;
  48. private string fileName;
  49. private bool isActive;
  50. private int percent;
  51. private long readLength;
  52. private string uploadID;
  53. private UploadState state;
  54. #endregion
  55. #region Properties
  56. internal DateTime BeginTime
  57. {
  58. get { return this.beginTime; }
  59. }
  60. public int FileCount
  61. {
  62. get { return this.fileCount; }
  63. set { this.fileCount = value; }
  64. }
  65. public long FileLength
  66. {
  67. get { return this.fileLength; }
  68. set { this.fileLength = value; }
  69. }
  70. public string FileName
  71. {
  72. get { return this.fileName; }
  73. set { this.fileName = value; }
  74. }
  75. public bool IsActive
  76. {
  77. get { return this.isActive; }
  78. }
  79. public TimeSpan LeftTime
  80. {
  81. get
  82. {
  83. TimeSpan span = TimeSpan.MaxValue;
  84. if ((this.fileLength - this.readLength) > 0)
  85. {
  86. span = new TimeSpan(0, 0, ((int) Math.Round((this.fileLength - this.readLength)/this.Speed, 0)));
  87. }
  88. return span;
  89. }
  90. }
  91. public int Percent
  92. {
  93. get
  94. {
  95. if (this.fileLength > 0)
  96. {
  97. this.percent = ((int) Math.Floor(((this.readLength*100)/this.fileLength)));
  98. }
  99. return this.percent;
  100. }
  101. }
  102. public long ReceivedLength
  103. {
  104. get { return this.readLength; }
  105. set
  106. {
  107. this.readLength = value;
  108. if (this.readLength < this.fileLength)
  109. {
  110. this.state = UploadState.Uploading;
  111. }
  112. }
  113. }
  114. public double Speed
  115. {
  116. get
  117. {
  118. TimeSpan span = DateTime.Now.Subtract(this.beginTime);
  119. double totalSeconds = span.TotalSeconds;
  120. double speed = 0;
  121. if ((totalSeconds == 0) && (this.readLength == this.fileLength))
  122. {
  123. totalSeconds = 1;
  124. }
  125. if (totalSeconds > 0)
  126. {
  127. speed = Math.Round(this.readLength/totalSeconds, 2);
  128. }
  129. return speed;
  130. }
  131. }
  132. public UploadState State
  133. {
  134. get { return this.state; }
  135. set { this.state = value; }
  136. }
  137. #endregion
  138. #region Constructor
  139. /// <summary>
  140. /// 
  141. /// </summary>
  142. internal UploadStatus()
  143. {
  144. this.fileLength = 0;
  145. this.readLength = 0;
  146. this.state = UploadState.Initializing;
  147. this.percent = 0;
  148. this.beginTime = DateTime.MinValue;
  149. this.isActive = true;
  150. this.uploadID = string.Empty;
  151. this.fileName = string.Empty;
  152. this.fileCount = 0;
  153. this.beginTime = DateTime.Now;
  154. }
  155. /// <summary>
  156. /// 
  157. /// </summary>
  158. /// <param name="UploadID"></param>
  159. public UploadStatus(string UploadID)
  160. {
  161. this.fileLength = 0;
  162. this.readLength = 0;
  163. this.state = UploadState.Initializing;
  164. this.percent = 0;
  165. this.beginTime = DateTime.MinValue;
  166. this.isActive = true;
  167. this.uploadID = string.Empty;
  168. this.fileName = string.Empty;
  169. this.fileCount = 0;
  170. this.uploadID = UploadID;
  171. UploadStatus uploadStatus = ((UploadStatus) Utils.GetContext().Application[("_UploadGUID_" + UploadID)]);
  172. if (uploadStatus != null)
  173. {
  174. this.fileLength = uploadStatus.fileLength;
  175. this.readLength = uploadStatus.ReceivedLength;
  176. this.percent = uploadStatus.Percent;
  177. this.state = uploadStatus.State;
  178. this.beginTime = uploadStatus.BeginTime;
  179. this.fileName = uploadStatus.FileName;
  180. this.fileCount = uploadStatus.FileCount;
  181. }
  182. this.isActive = (Utils.GetContext().Application[("_UploadGUID_" + UploadID)] != null);
  183. }
  184. #endregion
  185. /// <summary>
  186. /// Clear application variable.
  187. /// </summary>
  188. public void Dispose()
  189. {
  190. if (this.uploadID != string.Empty)
  191. {
  192. Utils.GetContext().Application.Remove(("_UploadGUID_" + this.uploadID));
  193. }
  194. }
  195. ~UploadStatus()
  196. {
  197. if (this.uploadID != string.Empty)
  198. {
  199. Utils.GetContext().Application.Remove(("_UploadGUID_" + this.uploadID));
  200. }
  201. }
  202. }
  203. }