
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

namespace NetRoute.Test_News.Comman


{
public class Common

{
public Common()

{

}

/**//// <summary>
/// 生成的文件名类别(guid,date两种)
/// </summary>
public enum FileNameType

{
Guid,
DateTime,
}
private static string _Message = string.Empty;//操作消息..
public static string Message

{

get
{ return _Message; }
}

static int Seed = 1;

/**//// <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)

{
bool _result = true;
if (_fu.HasFile)

{
if (FileSize != null)

{
if (_fu.PostedFile.ContentLength > FileSize)

{
_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")

{
try

{
if (Type == FileNameType.Guid)

{
string NewFileName = System.Guid.NewGuid().ToString() + "." + extend;
_fu.SaveAs(path + NewFileName);
}
else if (Type == FileNameType.DateTime)

{
Random rd = new Random(Seed);
string NewFileName = MakeFileName + "." + extend;
_fu.SaveAs(path + NewFileName);
}
}
catch (Exception ex)

{
_Message = ex.Message;
_result = false;
}
}
else

{
_Message = "上传的文件格式不正确!";
_result = false;
}
}
else

{
if (!NoFile)

{
_result = false;
_Message = "您没有上传文件!";
}

}
return _result;
}

private static int _Seed = 0;
private static string MakeFileName

{
get

{
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)

{
return UploadFile(_fu, path, null, NoFile, Type);
}
}
}

posted on
2009-01-20 10:32
danielduan
阅读(
735)
评论()
收藏
举报