Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace NetRoute.Test_News.Comman
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public class Common
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public Common()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 生成的文件名类别(guid,date两种)
/// </summary>
public enum FileNameType
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Guid,
DateTime,
}
private static string _Message = string.Empty;//操作消息..
public static string Message
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _Message; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
static int Seed = 1;
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 上传文件
/// </summary>
/// <param name="_fu">文件上传控件</param>
/// <param name="path">上传路径(不带文件名)</param>
/// <param name="FileSize">文件大小(以字节为单位)</param>
/// <param name="NoFile">是否允许空</param>
/// <param name="Type">生成的文件名类型(Guid/DateTime)</param>
/// <returns></returns>
public static bool UploadFile(FileUpload _fu,string path,int? FileSize,bool NoFile,FileNameType Type)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bool _result = true;
if (_fu.HasFile)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (FileSize != null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_fu.PostedFile.ContentLength > FileSize)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_Message = "上传的文件过大!";
_result = false;
return _result;
}
}
string FileName = _fu.FileName;
string FileType = _fu.PostedFile.ContentType;
string extend = FileName.Substring((FileName.LastIndexOf('.') + 1));
if (FileType.Substring(0,FileType.IndexOf('/')) == "image")
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (Type == FileNameType.Guid)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string NewFileName = System.Guid.NewGuid().ToString() + "." + extend;
_fu.SaveAs(path + NewFileName);
}
else if (Type == FileNameType.DateTime)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Random rd = new Random(Seed);
string NewFileName = MakeFileName + "." + extend;
_fu.SaveAs(path + NewFileName);
}
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_Message = ex.Message;
_result = false;
}
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_Message = "上传的文件格式不正确!";
_result = false;
}
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!NoFile)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_result = false;
_Message = "您没有上传文件!";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
return _result;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static int _Seed = 0;
private static string MakeFileName
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Random ran = new Random(_Seed);
_Seed++;
if (_Seed == 10000)
_Seed = 0;
return DateTime.Now.ToFileTime().ToString() + ran.Next(1, 999).ToString();
}
}
public static bool UploadFile(FileUpload _fu, string path, bool NoFile, FileNameType Type)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return UploadFile(_fu, path, null, NoFile, Type);
}
}
}
转载于:https://www.cnblogs.com/danielduan/archive/2009/01/20/1378742.html