asp.net2.0 上传类-带详细注释

开发环境:vs.net2005
给予网络上的一个上传类修改
调用代码:
MyUpload MyUpload 
=   new  MyUpload();
MyUpload.Path 
=   " upload " ;
MyUpload.Sizes 
=   2048 ;
MyUpload.FileType 
=   " jpg|gif|bmp " ;
MyUpload.PostedFile 
=  FileUpload1.PostedFile;
Label1.Text 
=  MyUpload.Upload();MyUpload.cs  using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

///   <summary>
///  MyUpload说明
///   </summary>
public   class  MyUpload
{
private   string  path  =   null ;
private   string  fileType  =   null ;
private   int  sizes  =   0 ;
private  HttpPostedFile postedFile  =   null ;
///   <summary>
///  初始化变量
///   </summary>
public  MyUpload()
{
}
//
public  HttpPostedFile PostedFile
{
get
{
return  postedFile;
}
set
{
postedFile 
=  value;
}
}
///   <summary>
///  设置上传路径,如:upload
///   </summary>
public   string  Path
{
get
{
return  path;
}
set
{
path 
=   @" "   +  value  +   @" " ;
}
}

///   <summary>
///  设置上传文件大小,单位为KB
///   </summary>
public   int  Sizes
{
get  
{
return  sizes;
}
set
{
sizes 
=  value  *   1024 ;
}
}

///   <summary>
///  上传文件的类型
///   </summary>
public   string  FileType
{
get
{
return  fileType;
}
set
{
fileType 
=  value;
}
}
public   string  PathToName( string  path)
{
int  pos  =  path.LastIndexOf( " / " );
return  path.Substring(pos  +   1 );


///   <summary>
///  上传
///   </summary>

public   string  Upload()
{
try
{
string  filePath  =   null ;
// 以当前时间修改图片的名字或创建文件夹的名字
string  modifyFileName  =  DateTime.Now.Year.ToString()  +  DateTime.Now.Month.ToString()  +  DateTime.Now.Day.ToString()  +  DateTime.Now.Hour.ToString()  +  DateTime.Now.Minute.ToString()  +  DateTime.Now.Second.ToString()  +  DateTime.Now.Millisecond.ToString();
// 获得站点的物理路径
string  uploadFilePath  =   null ;

uploadFilePath 
=  System.Web.HttpContext.Current.Server.MapPath( " . " +  path;

// 获得文件的上传的路径
string  sourcePath  =  PathToName(PostedFile.FileName);
// 判断上传文件是否为空
if  (sourcePath  ==   ""   ||  sourcePath  ==   null )
{
message(
" 您没有上传数据呀,是不是搞错了呀! " );
return   null ;
}
// 获得文件扩展名
string  tFileType  =  sourcePath.Substring(sourcePath.LastIndexOf( " . " +   1 );
// 获得上传文件的大小
long  strLen  =  PostedFile.ContentLength;
// 分解允许上传文件的格式
string [] temp  =  fileType.Split( ' | ' );
// 设置上传的文件是否是允许的格式
bool  flag  =   false ;
// 判断上传文件大小
if  (strLen  >=  sizes)
{

message(
" 上传的图片不能大于 "   +  sizes  +   " KB " );
return   null ;
}
// 判断上传的文件是否是允许的格式
foreach  ( string  data  in  temp)
{
if  (data  ==  tFileType)
{
flag 
=   true ;
break ;
}
}
// 如果为真允许上传,为假则不允许上传
if  ( ! flag)
{
message(
" 目前本系统支持的格式为: "   +  fileType);
return   null ;
}
System.IO.DirectoryInfo dir 
=   new  System.IO.DirectoryInfo(uploadFilePath);
// 判断文件夹否存在,不存在则创建
if  ( ! dir.Exists)
{
dir.Create();
}
filePath 
=  uploadFilePath  +  modifyFileName  +   " . "   +  tFileType;
PostedFile.SaveAs(filePath);
filePath 
=  path  +  modifyFileName  +   " . "   +  tFileType;

return  filePath;

}
catch
{
// 异常
message( " 出现未知错误! " );
return   null ;
}
}

private   void  message( string  msg,  string  url)
{
System.Web.HttpContext.Current.Response.Write(
" <script language=javascript>alert(' "   +  msg  +   " ');window.location=' "   +  url  +   " '</script> " );
}

private   void  message( string  msg)
{
System.Web.HttpContext.Current.Response.Write(
" <script language=javascript>alert(' "   +  msg  +   " ');</script> " );
}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值