📄 uploadstatus.cs
字号:
#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 WUSGControl.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; }
}
/// <summary>
/// 待查?
/// </summary>
public int FileCount
{
get { return this.fileCount; }
set { this.fileCount = value; }
}
/// <summary>
/// 文件长度,就是文件的二进制字节长度
/// </summary>
public long FileLength
{
get { return this.fileLength; }
set { this.fileLength = value; }
}
/// <summary>
/// 文件名字
/// </summary>
public string FileName
{
get { return this.fileName; }
set { this.fileName = value; }
}
/// <summary>
/// 是否是激活状态,Application[("_UploadGUID_" + UploadID)]为空时则为活动停止
/// </summary>
public bool IsActive
{
get { return this.isActive; }
}
/// <summary>
/// 还有多少时间没有传完
/// </summary>
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;
}
}
/// <summary>
/// 完成百分比
/// </summary>
public int Percent
{
get
{
if (this.fileLength > 0)
{
this.percent = ((int) Math.Floor((double)((this.readLength*100)/this.fileLength)));
}
return this.percent;
}
}
/// <summary>
/// 已读和接收字节的长度
/// </summary>
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;
}
}
/// <summary>
/// 上传的状态,是UploadState枚举类型
/// </summary>
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">文件上传ID,产生方式为: 格式为:</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;
//在全局中找状态.产生方式: 格式为:
//if (System.Web.HttpContext.Current != null)
//{
UploadStatus uploadStatus = ((UploadStatus)System.Web.HttpContext.Current.Application[("_UploadGUID_" + UploadID)]);
//Utils.2writem("UploadStatus 在初始化时:" + ("_UploadGUID_" + UploadID));
if (uploadStatus != null)
{
//Utils.2writem("初始化时在Application中找到历史状态对象!");
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 = (System.Web.HttpContext.Current.Application[("_UploadGUID_" + UploadID)] != null);
//}
//this.isActive = false;
}
#endregion
/// <summary>
/// Clear application variable.
/// </summary>
public void Dispose()
{
if (this.uploadID != string.Empty&&Utils.GetContext() != null)
{
Utils.GetContext().Application.Remove(("_UploadGUID_" + this.uploadID));
}
}
/// <summary>
/// 生新定义了对象的析构方法
/// </summary>
~UploadStatus()
{
if (this.uploadID != string.Empty&&Utils.GetContext() != null)
{
Utils.GetContext().Application.Remove(("_UploadGUID_" + this.uploadID));
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -