UploadStatus.cs
上传用户:whjkdz
上传日期:2013-05-19
资源大小:79k
文件大小:5k
- #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;
- namespace Sunrise.Web.Upload
- {
- /// <summary>
- /// Enum of upload state.
- /// </summary>
- public enum UploadState : byte
- {
- Error = 5,
- Completed = 4,
- Initializing = 0,
- Moving = 3,
- Uploaded = 2,
- Uploading = 1
- }
- public class UploadStatus : IDisposable
- {
- #region Fields
- private DateTime beginTime;
- private int fileCount;
- private long fileLength;
- private string fileName;
- private bool isActive;
- private int percent;
- private long readLength;
- private string uploadID;
- private UploadState state;
- #endregion
- #region Properties
- internal DateTime BeginTime
- {
- get { return this.beginTime; }
- }
- public int FileCount
- {
- get { return this.fileCount; }
- set { this.fileCount = value; }
- }
- public long FileLength
- {
- get { return this.fileLength; }
- set { this.fileLength = value; }
- }
- public string FileName
- {
- get { return this.fileName; }
- set { this.fileName = value; }
- }
- public bool IsActive
- {
- get { return this.isActive; }
- }
- public TimeSpan LeftTime
- {
- get
- {
- TimeSpan span = TimeSpan.MaxValue;
- if ((this.fileLength - this.readLength) > 0)
- {
- span = new TimeSpan(0, 0, ((int) Math.Round((this.fileLength - this.readLength)/this.Speed, 0)));
- }
- return span;
- }
- }
- public int Percent
- {
- get
- {
- if (this.fileLength > 0)
- {
- this.percent = ((int) Math.Floor(((this.readLength*100)/this.fileLength)));
- }
- return this.percent;
- }
- }
- public long ReceivedLength
- {
- get { return this.readLength; }
- set
- {
- this.readLength = value;
- if (this.readLength < this.fileLength)
- {
- this.state = UploadState.Uploading;
- }
- }
- }
- public double Speed
- {
- get
- {
- TimeSpan span = DateTime.Now.Subtract(this.beginTime);
- double totalSeconds = span.TotalSeconds;
- double speed = 0;
- if ((totalSeconds == 0) && (this.readLength == this.fileLength))
- {
- totalSeconds = 1;
- }
- if (totalSeconds > 0)
- {
- speed = Math.Round(this.readLength/totalSeconds, 2);
- }
- return speed;
- }
- }
- public UploadState State
- {
- get { return this.state; }
- set { this.state = value; }
- }
- #endregion
- #region Constructor
- /// <summary>
- ///
- /// </summary>
- internal UploadStatus()
- {
- this.fileLength = 0;
- this.readLength = 0;
- this.state = UploadState.Initializing;
- this.percent = 0;
- this.beginTime = DateTime.MinValue;
- this.isActive = true;
- this.uploadID = string.Empty;
- this.fileName = string.Empty;
- this.fileCount = 0;
- this.beginTime = DateTime.Now;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="UploadID"></param>
- public UploadStatus(string UploadID)
- {
- this.fileLength = 0;
- this.readLength = 0;
- this.state = UploadState.Initializing;
- this.percent = 0;
- this.beginTime = DateTime.MinValue;
- this.isActive = true;
- this.uploadID = string.Empty;
- this.fileName = string.Empty;
- this.fileCount = 0;
- this.uploadID = UploadID;
- UploadStatus uploadStatus = ((UploadStatus) Utils.GetContext().Application[("_UploadGUID_" + UploadID)]);
- if (uploadStatus != null)
- {
- this.fileLength = uploadStatus.fileLength;
- this.readLength = uploadStatus.ReceivedLength;
- this.percent = uploadStatus.Percent;
- this.state = uploadStatus.State;
- this.beginTime = uploadStatus.BeginTime;
- this.fileName = uploadStatus.FileName;
- this.fileCount = uploadStatus.FileCount;
- }
- this.isActive = (Utils.GetContext().Application[("_UploadGUID_" + UploadID)] != null);
- }
- #endregion
- /// <summary>
- /// Clear application variable.
- /// </summary>
- public void Dispose()
- {
- if (this.uploadID != string.Empty)
- {
- Utils.GetContext().Application.Remove(("_UploadGUID_" + this.uploadID));
- }
- }
- ~UploadStatus()
- {
- if (this.uploadID != string.Empty)
- {
- Utils.GetContext().Application.Remove(("_UploadGUID_" + this.uploadID));
- }
- }
- }
- }