收藏的asp.net文件上传类源码

 更新时间:2007年03月12日 00:00:00   作者:  
namespace Wmj 

public class MyUpload 

private System.Web.HttpPostedFile postedFile=null; 
private string savePath=""; 
private string extension=""; 
private int fileLength=0; 
//显示该组件使用的参数信息 
public string Help 

get{ 
string helpstring; 
helpstring="<font size=3>MyUpload myUpload=new MyUpload(); //构造函数"; 
helpstring+="myUpload.PostedFile=file1.PostedFile;//设置要上传的文件"; 
helpstring+="myUpload.SavePath=\"e:\\\";//设置要上传到服务器的路径,默认c:\\"; 
helpstring+="myUpload.FileLength=100; //设置上传文件的最大长度,单位k,默认1k"; 
helpstring+="myUpload.Extension=\"doc\";设置上传文件的扩展名,默认txt"; 
helpstring+="label1.Text=myUpload.Upload();//开始上传,并显示上传结果</font>"; 
helpstring+="<font size=3 color=red>Design By WengMingJun 2001-12-12 All Right Reserved!</font>"; 
return helpstring; 


public System.Web.HttpPostedFile PostedFile 

get 

return postedFile; 

set 

postedFile=value; 


public string SavePath 

get 

if(savePath!="") return savePath; 
return "c:\\"; 

set 

savePath=value; 


public int FileLength 

get 

if(fileLength!=0) return fileLength; 
return 1024; 

set 

fileLength=value*1024; 


public string Extension 

get 

if(extension!="") return extension; 
return "txt"; 

set 

extension=value; 


public string PathToName(string path) 

int pos=path.LastIndexOf("\\"); 
return path.Substring(pos+1); 

public string Upload() 

if(PostedFile!=null) 

try{ 
string fileName=PathToName(PostedFile.FileName); 
if(!fileName.EndsWith(Extension)) return "You must select "+Extension+" file!"; 
if(PostedFile.ContentLength>FileLength) return "File too big!"; 
PostedFile.SaveAs(SavePath+fileName); 
return "Upload File Successfully!"; 

catch(System.Exception exc) 
{return exc.Message;} 

return "Please select a file to upload!"; 



用csc /target:Library Wmj.cs 编译成dll供以后多次调用 
调用举例 
<%@page language="C#" runat="server"%> 
<%@import namespace="Wmj"%> 
<script language="C#" runat="server"> 
void Upload(object sender,EventArgs e) 

MyUpload myUpload=new MyUpload(); 
// label1.Text=myUpload.Help; 
myUpload.PostedFile=file1.PostedFile; 
myUpload.SavePath="e:\\"; 
myUpload.FileLength=100; 
label1.Text=myUpload.Upload(); 

</script> 
<form enctype="multipart/form-data" runat="server"> 
<input type="file" id="file1" runat="server"/> 
<asp:Button id="button1" Text="Upload" OnClick="Upload" runat="server"/> 
<asp:Label id="label1" runat="server"/> 
</form> 

相关文章

  • asp.net FindControl方法误区和解析

    asp.net FindControl方法误区和解析

    在ASP.NET中Control都有一个FindControl方法,其作用是根据ID(注意既不是UniqueID也不是ClientID)在Control所在的命名容器中寻找相应控件,但实际使用中存在很多误区和陷阱,下面谈谈个人对此的理解
    2012-01-01
  • .Net创建型设计模式之原型模式(Prototype)

    .Net创建型设计模式之原型模式(Prototype)

    这篇文章介绍了.Net设计模式之原型模式(Prototype),文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • asp.net平台下C#实现Socket通信

    asp.net平台下C#实现Socket通信

    这篇文章介绍了asp.net平台下C#实现Socket通信的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-01-01
  • .Net行为型设计模式之备忘录模式(Memento)

    .Net行为型设计模式之备忘录模式(Memento)

    这篇文章介绍了.Net行为型设计模式之备忘录模式(Memento),文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • SqlConnection.ConnectionString相关关键字

    SqlConnection.ConnectionString相关关键字

    SqlConnection.ConnectionString相关关键字...
    2007-01-01
  • ASP.NET MVC中两个配置文件的作用详解

    ASP.NET MVC中两个配置文件的作用详解

    本文详细讲解了ASP.NET MVC中两个配置文件的作用,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-03-03
  • .Net行为型设计模式之解释器模式(Interpreter)

    .Net行为型设计模式之解释器模式(Interpreter)

    这篇文章介绍了.Net行为型设计模式之解释器模式(Interpreter),文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • ASP.NET 入门的五个步骤

    ASP.NET 入门的五个步骤

    ASP.NET 入门的五个步骤...
    2007-01-01
  • .NET Core使用CZGL.SystemInfo库获取主机运行资源

    .NET Core使用CZGL.SystemInfo库获取主机运行资源

    这篇文章介绍了.NET Core使用CZGL.SystemInfo库获取主机运行资源的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-01-01
  • WPF框架Prism中ViewModelLocator用法介绍

    WPF框架Prism中ViewModelLocator用法介绍

    这篇文章介绍了WPF框架Prism中ViewModelLocator的用法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-02-02

最新评论