Files
g.hnyhua.cn/Mtxfw.Utility/UploadFile.cs
2026-02-07 15:48:27 +08:00

238 lines
7.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
namespace Mtxfw.Utility
{
/// <summary>
/// 文件上传 By Telesa 2011.02.29
/// </summary>
public class UploadFile
{
#region
private HttpPostedFile _HttpPostedFile;
private string _InPath;
private string _UpType;
private bool _IsBool;
private string _UploadPath;
private string _Message;
private int _UpSize;
private string _FileName;
private long _FileSize;
private string _FileType;
#endregion
#region
/// <summary>
/// 构造函数
/// </summary>
/// <param name="isControl">上传控件</param>
/// <param name="isPath">上传路径</param>
/// <param name="upType">上传类型逗号分隔:jpg,gif,bmp</param>
public UploadFile(HttpPostedFile PostFile, string isPath, string upType, int upSize)
{
this._HttpPostedFile = PostFile;
this._InPath = isPath;
this._UpType = upType;
this._UpSize = upSize;
}
#endregion
#region
/// <summary>
/// 生成GUID字符串
/// </summary>
private string getGuidString()
{
return Guid.NewGuid().ToString().Replace("-", "").ToUpper();
}
/// <summary>
/// 获取上传文件的扩展名 不带 "."
/// </summary>
private string getExtension(string file)
{
string name = Path.GetExtension(_HttpPostedFile.FileName);
return name.Substring(name.LastIndexOf(".") + 1).ToLower();
}
/// <summary>
/// 获取上传文件的原始名称
/// </summary>
private string getName(string file)
{
return file.Substring(file.LastIndexOf("/") + 1).ToLower();
}
/// <summary>
/// 判断上传的文件类型是否是允许的
/// </summary>
private bool hasType(string[] array, string type)
{
bool isType = false;
foreach (string str in array)
{
if (type.IndexOf(str) != -1)
{
isType = true;
//如果上传的文件类型是允许的则跳出循环
break;
}
}
return isType;
}
//按年月日生产文件夹 格式 \YYYY\MM\DD
private string getDateString()
{
StringBuilder sb = new StringBuilder();
sb.Append(DateTime.Now.Year);
sb.Append("-");
sb.Append(DateTime.Now.Month);
sb.Append("-");
sb.Append(DateTime.Now.Day);
sb.Append("/");
return sb.ToString();
}
#endregion
#region
/// <summary>
/// 上传文件 并判断是否上传成功
/// </summary>
/// <returns></returns>
public bool StatrUpFile()
{
string isFileName = getExtension(_HttpPostedFile.FileName); //获取文件扩展名
_FileType = getExtension(_HttpPostedFile.FileName);
_FileSize = _HttpPostedFile.ContentLength;
string strContentType = _HttpPostedFile.ContentType.ToLower();
//获取文件的原始文件名
_FileName = getName(_HttpPostedFile.FileName);
string[] typeList = _UpType.Split(','); //限制上传的类型集合
_IsBool = hasType(typeList, isFileName);
if (!String.IsNullOrEmpty(_HttpPostedFile.FileName))
{
if (_HttpPostedFile.ContentLength < (_UpSize * 1024))
{
if (_IsBool)
{
if (strContentType.IndexOf("jpg") != -1 || strContentType.IndexOf("jpeg") != -1 || strContentType.IndexOf("gif") != -1 || strContentType.IndexOf("png") != -1 || strContentType.IndexOf("bmp") != -1)
{
string FileType = _HttpPostedFile.FileName.Substring(_HttpPostedFile.FileName.LastIndexOf(".")).ToLower();
try
{
string DateString = getDateString();
string GuidName = getGuidString();
string InPathName = _InPath + DateString;
//如果文件夹不存在则创建
if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(InPathName)))
{
Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(InPathName));
}
//创建GUID图片名
string Final_Name = string.Format("{0}.{1}", GuidName, isFileName);
//上传
_HttpPostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath(InPathName + Final_Name));
if (!Mtxfw.Utility.Common.IsAllowedExtension(System.Web.HttpContext.Current.Server.MapPath(InPathName + Final_Name)))
{
File.Delete(System.Web.HttpContext.Current.Server.MapPath(InPathName + Final_Name));
_Message = "";
return false;
}
else
{
_UploadPath = DateString + Final_Name;
return true;
}
}
catch (Exception e)
{
_Message = e.Message;
return false;
}
}
else
{
_Message = "文件上传格式错误哦,请上传: " + _UpType + " 格式文件";
return false;
}
}
else
{
_Message = "文件上传格式错误哦,请上传: " + _UpType + " 格式文件";
return false;
}
}
else
{
_Message = "上传的文件不能大于 " + _UpSize + " KB";
return false;
}
}
else
{
_Message = "请选择要上传的文件!";
return false;
}
}
#endregion
#region
/// <summary>
/// 获取文件上传后的路径
/// </summary>
public string UploadPath
{
get { return _UploadPath; }
}
/// <summary>
/// 异常信息
/// </summary>
public string Message
{
get { return _Message; }
}
/// <summary>
/// 获取上传文件的原始文件名
/// </summary>
public string FileName
{
get { return _FileName; }
}
/// <summary>
/// 获取上传文件的类型
/// </summary>
public string FileType
{
get { return _FileType; }
}
/// <summary>
/// 获取上传文件字节流的长度
/// </summary>
public long FileSize
{
get { return _FileSize; }
}
#endregion
}
}